Esempio n. 1
0
		"helper",
	},

	// @todo "helpers hash - the helper hash should augment the global hash"

	// @todo "registration"

	{
		"decimal number literals work",
		"Message: {{hello -1.2 1.2}}",
		nil, nil,
		map[string]interface{}{"hello": func(times, times2 interface{}) string {
			ts, t2s := "NaN", "NaN"

			if v, ok := times.(float64); ok {
				ts = raymond.Str(v)
			}

			if v, ok := times2.(float64); ok {
				t2s = raymond.Str(v)
			}

			return "Hello " + ts + " " + t2s + " times"
		}},
		nil,
		"Message: Hello -1.2 1.2 times",
	},
	{
		"negative number literals work",
		"Message: {{hello -12}}",
		nil, nil,
Esempio n. 2
0
func equalHelper(a, b string) string {
	return raymond.Str(a == b)
}
Esempio n. 3
0
func launchTests(t *testing.T, tests []Test) {
	t.Parallel()

	for _, test := range tests {
		var err error
		var tpl *raymond.Template

		if DUMP_TPL {
			filename := strconv.Itoa(dump_tpl_nb)
			if err := ioutil.WriteFile(path.Join(".", "dump_tpl", filename), []byte(test.input), 0644); err != nil {
				panic(err)
			}
			dump_tpl_nb += 1
		}

		// parse template
		tpl, err = raymond.Parse(test.input)
		if err != nil {
			t.Errorf("Test '%s' failed - Failed to parse template\ninput:\n\t'%s'\nerror:\n\t%s", test.name, test.input, err)
		} else {
			if len(test.helpers) > 0 {
				// register helpers
				tpl.RegisterHelpers(test.helpers)
			}

			if len(test.partials) > 0 {
				// register partials
				tpl.RegisterPartials(test.partials)
			}

			// setup private data frame
			var privData *raymond.DataFrame
			if test.privData != nil {
				privData = raymond.NewDataFrame()
				for k, v := range test.privData {
					privData.Set(k, v)
				}
			}

			// render template
			output, err := tpl.ExecWith(test.data, privData)
			if err != nil {
				t.Errorf("Test '%s' failed\ninput:\n\t'%s'\ndata:\n\t%s\nerror:\n\t%s\nAST:\n\t%s", test.name, test.input, raymond.Str(test.data), err, tpl.PrintAST())
			} else {
				// check output
				var expectedArr []string
				expectedArr, ok := test.output.([]string)
				if ok {
					match := false
					for _, expectedStr := range expectedArr {
						if expectedStr == output {
							match = true
							break
						}
					}

					if !match {
						t.Errorf("Test '%s' failed\ninput:\n\t'%s'\ndata:\n\t%s\npartials:\n\t%s\nexpected\n\t%q\ngot\n\t%q\nAST:\n%s", test.name, test.input, raymond.Str(test.data), raymond.Str(test.partials), expectedArr, output, tpl.PrintAST())
					}
				} else {
					expectedStr, ok := test.output.(string)
					if !ok {
						panic(fmt.Errorf("Erroneous test output description: %q", test.output))
					}

					if expectedStr != output {
						t.Errorf("Test '%s' failed\ninput:\n\t'%s'\ndata:\n\t%s\npartials:\n\t%s\nexpected\n\t%q\ngot\n\t%q\nAST:\n%s", test.name, test.input, raymond.Str(test.data), raymond.Str(test.partials), expectedStr, output, tpl.PrintAST())
					}
				}
			}
		}
	}
}