Ejemplo n.º 1
0
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")
}
Ejemplo n.º 2
0
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

}
Ejemplo n.º 3
0
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")

}
Ejemplo n.º 4
0
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")
}
Ejemplo n.º 5
0
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")

}
Ejemplo n.º 6
0
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")

}
Ejemplo n.º 7
0
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")

}
Ejemplo n.º 8
0
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")

}
Ejemplo n.º 9
0
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")

}
Ejemplo n.º 10
0
Archivo: int_test.go Proyecto: 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")

}
Ejemplo n.º 11
0
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)
}
Ejemplo n.º 12
0
Archivo: run_test.go Proyecto: 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)

}
Ejemplo n.º 13
0
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")
}