Exemplo n.º 1
0
func testGenerated(t *testing.T, path string) {
	cb := tests.New().Path(path).Jauto().Sauto(parser.Parse)
	pi, err := parser.Parse(cb.Ctx(), path)
	require.NoError(t, err)

	generatedBytes, err := generate.Structs(cb.Ctx(), pi.Env)
	require.NoError(t, err)
	generatedString := string(generatedBytes)

	existingFilePath := filepath.Join(pi.Dir, "generated.go")
	existingBytes, err := ioutil.ReadFile(existingFilePath)
	require.NoError(t, err)
	existingString := string(existingBytes)

	// TODO: The "goimports" tool will often re-order the imports, so this is
	// a kludge to remove it before comparing. This is not ideal!
	importsRegex := regexp.MustCompile(`(?ms:\nimport \(\n.*\n\)\n)`)
	generatedString = importsRegex.ReplaceAllString(generatedString, "-")
	existingString = importsRegex.ReplaceAllString(existingString, "-")

	if generatedString != existingString {
		fmt.Println("Generated code for " + path + " is not what is present:")
		fmt.Println(generatedString)
	}
	require.Equal(t, generatedString, existingString)
}
Exemplo n.º 2
0
Arquivo: generate.go Projeto: kego/ke
// Generate generates the source code for type structs, and writes the
// generated.go to the filesystem.
func Generate(ctx context.Context, env *envctx.Env) error {

	wgctx.Add(ctx, "Generate")
	defer wgctx.Done(ctx, "Generate")

	cmd := cmdctx.FromContext(ctx)

	cmd.Printf("Generating types for %s... ", env.Path)

	outputDir := env.Dir
	filename := "generated.go"
	source, err := generate.Structs(ctx, env)
	if err != nil {
		return kerr.Wrap("XFNESBLBTQ", err)
	}

	// We only backup in the system structs and types files because they are
	// the only generated files we ever need to roll back
	backup := env.Path == "kego.io/system"

	if err = save(outputDir, source, filename, backup); err != nil {
		return kerr.Wrap("UONJTTSTWW", err)
	} else {
		cmd.Println("OK.")
	}

	return nil
}
Exemplo n.º 3
0
Arquivo: ext.go Projeto: kego/ke
func InitialiseAndGenerate(t *testing.T, cb *tests.ContextBuilder, name string, files map[string]string) (string, error) {
	path, _ := cb.TempPackage(name, files)
	ctx, _, err := process.Initialise(cb.Ctx(), &process.Options{
		Path: path,
	})
	if err != nil {
		return "", err
	}
	cb.SetCtx(ctx)
	source, err := generate.Structs(ctx, cb.Env())
	if err != nil {
		return "", err
	}
	return string(source), nil
}
Exemplo n.º 4
0
func TestParse(t *testing.T) {

	cb := tests.New().TempGopath(true)
	defer cb.Cleanup()

	files := map[string]string{
		"a.json": `{
			"type": "system:type",
			"id": "a",
			"rule": {
				"type": "system:type",
				"embed": ["system:rule"],
				"fields": {
					"a": {
						"type": "system:@bool"
					}
				}
			}
		}`,
		"b.json": `{
			"type": "system:type",
			"id": "b",
			"fields": {
				"c": {
					"type": "@a",
					"a": true,
					"optional": true
				},
				"d": {
					"type": "@b"
				}
			}
		}`,
	}
	path, dir := cb.TempPackage("a", files)

	path = "kego.io/system"

	cb.Path(path).Dir(dir).Cmd().Sempty().Jsystem()

	pi, err := Parse(cb.Ctx(), path)
	require.NoError(t, err)

	_, err = generate.Structs(cb.Ctx(), pi.Env)
	require.NoError(t, err)

}
Exemplo n.º 5
0
func TestGenerateSourceNoTypes(t *testing.T) {
	cb := tests.Context("a.b/c").Spkg("a.b/c")
	_, err := generate.Structs(cb.Ctx(), cb.Env())
	require.NoError(t, err)
}
Exemplo n.º 6
0
func TestGenerateSourceErr1(t *testing.T) {
	cb := tests.Context("a.b/c").Sempty()
	_, err := generate.Structs(cb.Ctx(), cb.Env())
	assert.IsError(t, err, "DQVQWTKRSK")
}