예제 #1
0
파일: parser_test.go 프로젝트: kego/ke
func TestRuleId(t *testing.T) {

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

	pathA, dirA := cb.TempPackage("a", map[string]string{
		"a.json": `{
			"type": "system:type",
			"id": "a",
			"rule": {
				"type": "system:type",
				"id": "foo"
			}
		}`,
	})
	cb.Path(pathA).Dir(dirA).Cmd().Sempty().Jsystem()
	_, err := Parse(cb.Ctx(), pathA)
	assert.IsError(t, err, "VFUNPHUFHD")
	assert.HasError(t, err, "JKARKEDTIW")

	cb.TempFile("a.json", `{
			"type": "system:type",
			"id": "a",
			"rule": {
				"type": "system:type"
			}
		}`)
	_, err = Parse(cb.Ctx(), pathA)
	assert.IsError(t, err, "VFUNPHUFHD")
	assert.HasError(t, err, "LMALEMKFDI")
}
예제 #2
0
파일: parser_test.go 프로젝트: kego/ke
func TestScan_errors(t *testing.T) {

	cb := tests.Context("").Cmd().Sempty().Jsystem().TempGopath(true)
	defer cb.Cleanup()

	_, err := Parse(cb.Ctx(), "does-not-exist")
	require.IsError(t, err, "SBALWXUPKN") // Error from ScanForEnv
	assert.HasError(t, err, "NIKCKQAKUI") // GoGet: exit status 1: package does-not-exist

	path, _ := cb.TempPackage("a", map[string]string{
		"a.json": `{
			"type": "system:package",
			"recursive": false
		}`,
		"b.json": "foo",
	})

	cb.Path(path)

	_, err = Parse(cb.Ctx(), path)
	assert.IsError(t, err, "VFUNPHUFHD")  // Error from scanForTypes
	assert.HasError(t, err, "HCYGNBDFFA") // Error trying to unmarshal a type

	path, _ = cb.TempPackage("a", map[string]string{
		"a.json": "foo",
	})

	cb.Path(path)

	_, err = Parse(cb.Ctx(), path)
	assert.IsError(t, err, "GJRHNGGWFD")  // Error from ScanForEnv
	assert.HasError(t, err, "MTDCXBYBEJ") // Error trying to scan for packages

}
예제 #3
0
파일: parser_test.go 프로젝트: kego/ke
func TestCircularImport(t *testing.T) {

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

	pathA, dirA := cb.TempPackage("a", map[string]string{})
	pathB, dirB := cb.TempPackage("b", map[string]string{})
	ioutil.WriteFile(filepath.Join(dirA, "package.json"), []byte(`
			{
				"type": "system:package",
				"aliases": {
					"b": "`+pathB+`"
				}
			}
		`), 0777)
	ioutil.WriteFile(filepath.Join(dirB, "package.json"), []byte(`
			{
				"type": "system:package",
				"aliases": {
					"a": "`+pathA+`"
				}
			}
		`), 0777)

	cb.Path(pathA).Dir(dirA).Cmd().Sempty().Jsystem()

	_, err := Parse(cb.Ctx(), pathA)
	assert.IsError(t, err, "NOVMGYKHHI")
	assert.HasError(t, err, "SCSCFJPPHD")

}
예제 #4
0
파일: node_ext_test.go 프로젝트: kego/ke
func TestNode_SetValueZero3(t *testing.T) {
	cb, n := data.Empty(t)
	f, ok := system.GetTypeFromCache(cb.Ctx(), "kego.io/tests/data", "face")
	assert.True(t, ok)
	err := n.SetValueZero(cb.Ctx(), true, f)
	assert.HasError(t, err, "VHOSYBMDQL")
}
예제 #5
0
파일: selectors_test.go 프로젝트: kego/ke
func TestGetNodes(t *testing.T) {
	p := getParser(t)

	out, err := p.GetNodes(":root")
	require.NoError(t, err)
	assert.Equal(t, 1, len(out))

	// The lexer extracts a S_WORD token, which has no selector
	out, err = p.GetNodes("\"foo\"")
	assert.IsError(t, err, "QFXXVGGHSS")
	assert.HasError(t, err, "REPJTLCAMQ")

	// The lexer extracts a S_FLOAT token, which has no selector
	out, err = p.GetNodes("1.1")
	assert.IsError(t, err, "QFXXVGGHSS")
	assert.HasError(t, err, "REPJTLCAMQ")

	_, err = p.GetNodes(".a * * .b")
	assert.IsError(t, err, "QFXXVGGHSS")
	assert.HasError(t, err, "KLEORWJHSP")

}
예제 #6
0
파일: node_ext_test.go 프로젝트: kego/ke
func TestNode_Unpack3(t *testing.T) {

	cb := tests.Context("kego.io/tests/data").Jauto().Sauto(parser.Parse)

	s := `{
	"type": "multi",
	"a": "b"
}`

	n := node.NewNode()
	err := n.Unpack(cb.Ctx(), system.MustPackString(s), false)
	assert.HasError(t, err, "SRANLETJRS")

}
예제 #7
0
파일: parser_test.go 프로젝트: kego/ke
func TestNoIdError(t *testing.T) {

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

	pathA, dirA := cb.TempPackage("a", map[string]string{
		"a.json": `{
			"type": "system:type"
		}`,
	})
	cb.Path(pathA).Dir(dirA).Cmd().Sempty().Jsystem()
	_, err := Parse(cb.Ctx(), pathA)
	assert.IsError(t, err, "VFUNPHUFHD")
	assert.HasError(t, err, "DLLMKTDYFW")

}
예제 #8
0
파일: parser_test.go 프로젝트: kego/ke
func TestTypesUnknownType(t *testing.T) {

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

	pathA, dirA := cb.TempPackage("a", map[string]string{
		"a.json": `{
			"type": "system:type",
			"id": "a",
			"rules": [
				{
					"type": "system:foo"
				}
			]
		}`,
	})
	cb.Path(pathA).Dir(dirA).Cmd().Sempty().Jsystem()
	_, err := Parse(cb.Ctx(), pathA)
	// unknown types are tolerated when scanning types
	require.NoError(t, err)

	cb.TempFile("b.json", `{
			"type": "system:package",
			"rules": [
				{
					"type": "foo:bar"
				}
			]
		}`)
	_, err = Parse(cb.Ctx(), pathA)
	// b.json is not a type so unknown packages and types are permitted
	require.NoError(t, err)

	cb.TempFile("c.json", `{
			"type": "system:type",
			"id": "c",
			"rules": [
				{
					"type": "foo:bar"
				}
			]
		}`)
	_, err = Parse(cb.Ctx(), pathA)
	// unknown packages are not tolerated when scanning types
	assert.HasError(t, err, "DKKFLKDKYI")

}
예제 #9
0
파일: parser_test.go 프로젝트: kego/ke
func TestNoTypeError(t *testing.T) {

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

	pathA, dirA := cb.TempPackage("a", map[string]string{
		"a.json": `{
			"id": "a"
		}`,
	})
	cb.Path(pathA).Dir(dirA).Cmd().Sempty().Jsystem()
	_, err := Parse(cb.Ctx(), pathA)
	assert.IsError(t, err, "GJRHNGGWFD")
	assert.HasError(t, err, "MSNIGTIDIO")

	err = scanForTypesAndExports(cb.Ctx(), cb.Env(), nil, &PackageHasher{})
	assert.IsError(t, err, "NUKWIHYFMQ")

}
예제 #10
0
파일: int_test.go 프로젝트: kego/ke
func TestIntUnmarshalJSON(t *testing.T) {

	var i *Int

	err := i.Unpack(envctx.Empty, Pack(nil), false)
	require.NoError(t, err)
	assert.Nil(t, i)

	i = NewInt(0)
	err = i.Unpack(envctx.Empty, Pack(2.0), false)
	require.NoError(t, err)
	assert.NotNil(t, i)
	assert.Equal(t, 2, i.Value())

	i = NewInt(0)
	err = i.Unpack(envctx.Empty, Pack(map[string]interface{}{
		"type":  "system:int",
		"value": 2.0,
	}), false)
	require.NoError(t, err)
	assert.NotNil(t, i)
	assert.Equal(t, 2, i.Value())

	i = NewInt(0)
	err = i.Unpack(envctx.Empty, Pack(-12.0), false)
	require.NoError(t, err)
	assert.NotNil(t, i)
	assert.Equal(t, -12, i.Value())

	i = NewInt(0)
	err = i.Unpack(envctx.Empty, Pack("foo"), false)
	assert.IsError(t, err, "UJUBDGVYGF")

	i = NewInt(0)
	err = i.Unpack(envctx.Empty, Pack(1.2), false)
	assert.HasError(t, err, "KVEOETSIJY")

}
예제 #11
0
파일: reference_test.go 프로젝트: kego/ke
func TestReferenceUnmarshal(t *testing.T) {

	reset := func() *Reference {
		// Let's pre-load with some values so we check that when we
		// load a null value, we clear all the fields
		r := NewReference("a.b/c", "d")
		return r
	}

	r := reset()
	err := r.Unpack(envctx.Empty, Pack(nil), false)
	assert.IsError(t, err, "MOQVSKJXRB")

	r = reset()
	err = r.Unpack(envctx.Empty, Pack(1.0), false)
	assert.IsError(t, err, "RFLQSBPMYM")

	r = reset()
	err = r.Unpack(envctx.Empty, Pack("a.b/c:d"), false)
	assert.IsError(t, err, "MSXBLEIGVJ")
	assert.HasError(t, err, "KJSOXDESFD")
	p, ok := kerr.Source(err).(UnknownPackageError)
	assert.True(t, ok)
	assert.Equal(t, "a.b/c", p.UnknownPackage)

	ctx := tests.Context("").Alias("c", "a.b/c").Ctx()

	r = reset()
	err = r.Unpack(ctx, Pack("a.b/c:d"), false)
	require.NoError(t, err)
	assert.NotNil(t, r)
	assert.Equal(t, "a.b/c", r.Package)
	assert.Equal(t, "d", r.Name)
	assert.Equal(t, "a.b/c:d", r.Value())

	r = reset()
	err = r.Unpack(ctx, Pack(map[string]interface{}{
		"type":  "system:reference",
		"value": "a.b/c:d",
	}), false)
	require.NoError(t, err)
	assert.NotNil(t, r)
	assert.Equal(t, "a.b/c", r.Package)
	assert.Equal(t, "d", r.Name)
	assert.Equal(t, "a.b/c:d", r.Value())

	r = reset()
	err = r.Unpack(ctx, Pack("a.b/c:@d"), false)
	require.NoError(t, err)
	assert.NotNil(t, r)
	assert.Equal(t, "a.b/c", r.Package)
	assert.Equal(t, "@d", r.Name)
	assert.Equal(t, "a.b/c:@d", r.Value())

	r = reset()
	err = r.Unpack(envctx.Empty, Pack("a:b"), false)
	assert.IsError(t, err, "MSXBLEIGVJ")
	assert.HasError(t, err, "DKKFLKDKYI")
	p, ok = kerr.Source(err).(UnknownPackageError)
	assert.True(t, ok)
	assert.Equal(t, "a", p.UnknownPackage)

	r = reset()
	err = r.UnmarshalInterface(envctx.Empty, 1)
	require.NoError(t, err)
	assert.Equal(t, *NewReference("", ""), *r)

	r = reset()
	err = r.UnmarshalInterface(envctx.Empty, "")
	require.NoError(t, err)
	assert.Equal(t, *NewReference("", ""), *r)

	r = reset()
	err = r.UnmarshalInterface(envctx.Empty, "a.b/c:d")
	assert.IsError(t, err, "ETLPLMMWCC")

	r = reset()
	err = r.UnmarshalInterface(tests.Context("a.b/c").Ctx(), "a.b/c:d")
	require.NoError(t, err)
	assert.Equal(t, *NewReference("a.b/c", "d"), *r)
}
예제 #12
0
파일: run_test.go 프로젝트: kego/ke
func TestRun(t *testing.T) {

	cb := tests.New().RealGopath()
	defer cb.Cleanup()

	path, dir := cb.TempPackage("d", map[string]string{
		"a.yaml": `
			type: system:type
			id: a
			fields:
				b:
					type: system:@string
					max-length: 5`,
		"d.go": `package d`,
	})

	cb.Path(path).Dir(dir).Jauto().Wg().Sauto(parser.Parse)

	env := envctx.FromContext(cb.Ctx())

	err := Generate(cb.Ctx(), env)
	require.NoError(t, err)

	b, err := ioutil.ReadFile(filepath.Join(dir, "generated.go"))
	require.NoError(t, err)
	assert.Contains(t, string(b), `pkg.Init(`)
	assert.Contains(t, string(b), `func() interface{} { return new(A) },`)
	assert.Contains(t, string(b), `func() interface{} { return new(ARule) },`)
	assert.Contains(t, string(b), `func() reflect.Type { return reflect.TypeOf((*AInterface)(nil)).Elem() },`)
	assert.Contains(t, string(b), fmt.Sprintf("%v", env.Hash))

	err = RunValidateCommand(cb.Ctx())
	require.NoError(t, err)

	file1, err := os.Stat(filepath.Join(dir, ".localke", "validate"))
	require.NoError(t, err)
	time1 := file1.ModTime()

	err = RunValidateCommand(cb.Ctx())
	require.NoError(t, err)

	cb.TempFile("c.yaml", `
		type: a
		id: c
		b: foo`)

	err = RunValidateCommand(cb.Ctx())
	require.NoError(t, err)

	// should not rebuild validate command
	file2, err := os.Stat(filepath.Join(dir, ".localke", "validate"))
	require.NoError(t, err)
	time2 := file2.ModTime()
	assert.Equal(t, time1, time2)

	cb.TempFile("e.yaml", `
		type: a
		id: e
		b: tooolong`)

	err = RunValidateCommand(cb.Ctx())
	assert.IsError(t, err, "KFNIOHWCBT")
	assert.HasError(t, err, "ETWHPXTUVB")

	cb.TempFile("f.yaml", `
		type: system:type
		id: f
		fields:
			a:
				type: system:@string`)

	// This loads the new system.Type into the system cache
	cb.Sauto(parser.Parse)
	// This generates a new generated.go
	err = Generate(cb.Ctx(), env)
	require.NoError(t, err)

	// This will re-run the build, but still return the validation error
	err = RunValidateCommand(cb.Ctx())
	assert.IsError(t, err, "KFNIOHWCBT")
	assert.HasError(t, err, "ETWHPXTUVB")

	cb.RemoveTempFile("e.yaml")

	cb.TempFile("h.yaml", `
		type: system:type
		id: h
		fields:
			a:
				type: system:@string`)

	// This loads the new system.Type into the system cache
	cb.Sauto(parser.Parse)
	// This generates a new generated.go
	err = Generate(cb.Ctx(), env)
	require.NoError(t, err)

	// This will re-run the build, but not return the validation error
	err = RunValidateCommand(cb.Ctx())
	require.NoError(t, err)

	// should rebuild validate command
	file3, err := os.Stat(filepath.Join(dir, ".localke", "validate"))
	require.NoError(t, err)
	time3 := file3.ModTime()
	assert.NotEqual(t, time1, time3)

	cb.TempFile("g.yaml", `
		type: system:type
		id: g
		fields:
			a:
				type: system:@string`)

	// We add a new type, but we haven't generated the struct, so it will fail with hash changed
	err = runValidateCommand(cb.Ctx(), false, false)
	assert.IsError(t, err, "DTTHRRJSSF")

	err = os.Remove(filepath.Join(dir, ".localke", "validate"))
	require.NoError(t, err)

	_, err = os.Stat(filepath.Join(dir, ".localke", "validate"))
	assert.True(t, os.IsNotExist(err))

	err = runValidateCommand(cb.Ctx(), false, false)
	assert.IsError(t, err, "DTTHRRJSSF")
	_, ok := kerr.Source(err).(*os.PathError)
	assert.True(t, ok)

}
예제 #13
0
파일: editor_test.go 프로젝트: kego/ke
func TestEditorErr(t *testing.T) {
	cb := tests.Context("a.b/c").Alias("a", "\"")
	_, err := Editor(cb.Ctx(), cb.Env())
	assert.IsError(t, err, "CBTOLUQOGL")
	assert.HasError(t, err, "CRBYOUOHPG")
}