Ejemplo n.º 1
0
Archivo: repacker.go Proyecto: kego/ke
func (*Repacker) Process(ctx context.Context, data []byte, i *interface{}) error {
	n, err := node.Unmarshal(ctx, data)
	if err != nil {
		return kerr.Wrap("RXAARYNHLP", err)
	}
	return system.Unpack(ctx, node.Pack(n), i)
}
Ejemplo n.º 2
0
Archivo: validate.go Proyecto: kego/ke
func validateBytes(ctx context.Context, bytes []byte) (errors []ValidationError, err error) {
	n, err := node.Unmarshal(ctx, bytes)
	if err != nil {
		return nil, kerr.Wrap("QIVNOQKCQF", err)
	}
	errors, err = ValidateNode(ctx, n)
	if err != nil {
		return nil, kerr.Wrap("RVKNMWKQHD", err)
	}
	return errors, nil
}
Ejemplo n.º 3
0
func getUtilParser(t *testing.T) *Parser {
	cb := tests.Context("a.b/c").Ssystem(parser.Parse)
	data := `{
		"type": "system:type",
		"id": "foo"
	}`
	n, err := node.Unmarshal(cb.Ctx(), []byte(data))
	require.NoError(t, err)
	p, err := CreateParser(cb.Ctx(), n)
	require.NoError(t, err)
	return p
}
Ejemplo n.º 4
0
Archivo: package.go Proyecto: kego/ke
func (s *PackageStore) Handle(payload *flux.Payload) bool {
	switch action := payload.Action.(type) {
	case *actions.InitialState:
		pkgNode, err := node.Unmarshal(s.ctx, action.Info.Package)
		if err != nil {
			s.app.Fail <- kerr.Wrap("KXIKEWOKJI", err)
		}
		s.node = pkgNode
		s.filename = action.Info.PackageFilename
		payload.Notify(nil, PackageChanged)
	}
	return true
}
Ejemplo n.º 5
0
func TestNodeUnpack(t *testing.T) {

	ctx, _, err := process.Initialise(context.Background(), &process.Options{
		Path: "kego.io/demo/site",
	})
	require.NoError(t, err)

	j := `{"type":"system:package","aliases":{"images":"kego.io/demo/common/images","units":"kego.io/demo/common/units","words":"kego.io/demo/common/words"}}`

	_, err = node.Unmarshal(ctx, []byte(j))
	require.NoError(t, err)

}
Ejemplo n.º 6
0
func TestNode_Print(t *testing.T) {

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

	s := `{
	"type": "simple",
	"js": "a"
}`

	n, err := node.Unmarshal(cb.Ctx(), []byte(s))
	require.NoError(t, err)
	require.Equal(t, `{"js":"a","type":"simple"}`, n.Print(cb.Ctx()))

}
Ejemplo n.º 7
0
func testUnpack(t *testing.T, path string) {
	ctx, _, err := process.Initialise(context.Background(), &process.Options{
		Path: path,
	})
	require.NoError(t, err)

	env := envctx.FromContext(ctx)

	files := scanner.ScanDirToFiles(ctx, env.Dir, env.Recursive)
	bytes := scanner.ScanFilesToBytes(ctx, files)
	for b := range bytes {
		_, err := node.Unmarshal(ctx, b.Bytes)
		require.NoError(t, err, b.File)
	}
}
Ejemplo n.º 8
0
func TestUnmarshal(t *testing.T) {

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

	s := `{
	"type": "simple",
	"js": "a"
}`

	n, err := node.Unmarshal(cb.Ctx(), []byte(s))
	require.NoError(t, err)
	m, ok := n.Value.(*data.Simple)
	require.True(t, ok)
	assert.Equal(t, "a", m.Js)

}
Ejemplo n.º 9
0
Archivo: type.go Proyecto: kego/ke
func (s *TypeStore) Handle(payload *flux.Payload) bool {
	switch action := payload.Action.(type) {
	case *actions.InitialState:
		types := action.Info.Imports[action.Info.Path].Types
		if len(types) == 0 {
			break
		}
		for name, ti := range types {
			typ, err := node.Unmarshal(s.ctx, ti.Bytes)
			if err != nil {
				s.app.Fail <- kerr.Wrap("QDOVEIKABS", err)
			}
			s.types[name] = &models.TypeModel{
				Node: typ,
				File: ti.File,
			}

		}
		payload.Notify(nil, TypeChanged)
	}
	return true
}
Ejemplo n.º 10
0
func runTestsInDirectory(t *testing.T, baseDirectory string) {
	var testDocuments = make(map[string]*node.Node)
	var testSelectors = make(map[string]string)
	var testOutput = make(map[string][]string)

	files, err := ioutil.ReadDir(baseDirectory)
	if err != nil {
		t.Error("Error encountered while loading conformance tests ", err)
	}

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

	for _, fileInfo := range files {
		name := fileInfo.Name()
		if strings.HasSuffix(name, ".json") {
			json_document, err := ioutil.ReadFile(baseDirectory + name)
			if err != nil {
				t.Error("Error encountered while reading ", name, ": ", err)
				continue
			}
			n, err := node.Unmarshal(cb.Ctx(), json_document)
			require.NoError(t, err, name)
			require.NotNil(t, n)

			testDocuments[name[0:len(name)-len(".json")]] = n
		} else if strings.HasSuffix(name, ".output") {
			output_document, err := ioutil.ReadFile(baseDirectory + name)
			if err != nil {
				t.Error("Error encountered while reading ", name, ": ", err)
				continue
			}
			if strings.HasPrefix(string(output_document), "Error") {
				// We won't be handling errors in the same way.
				continue
			}
			var actualOutput []string
			var stringTemporary string
			for _, str := range strings.Split(string(output_document), "\n") {
				stringTemporary = stringTemporary + str
				// Try to parse -- if it works, we have the whole object
				if strings.Index(stringTemporary, "{") == 0 {
					if strings.Count(stringTemporary, "{") != strings.Count(stringTemporary, "}") {
						continue
					}
					actualOutput = append(actualOutput, stringTemporary)
					stringTemporary = ""
				} else if strings.Index(stringTemporary, "[") == 0 {
					if strings.Count(stringTemporary, "[") != strings.Count(stringTemporary, "]") {
						continue
					}
					actualOutput = append(actualOutput, stringTemporary)
					stringTemporary = ""
				} else if len(stringTemporary) > 0 {
					actualOutput = append(actualOutput, stringTemporary)
					stringTemporary = ""
				}
			}
			testOutput[name[0:len(name)-len(".output")]] = actualOutput
		} else if strings.HasSuffix(name, ".selector") {
			selector_document, err := ioutil.ReadFile(baseDirectory + name)
			if err != nil {
				t.Error("Error encountered while reading ", name, ": ", err)
				continue
			}
			testSelectors[name[0:len(name)-len(".selector")]] = string(selector_document)
		}
	}

	for testName := range testOutput {
		single := ""
		if single != "" && testName != single {
			continue
		}
		var passed bool = true
		//t.Log("Running test ", testName)
		parser, err := getTestParser(cb.Ctx(), testDocuments, testName)
		if err != nil {
			t.Error("Test ", testName, "failed: ", err)
			passed = false
		}
		selectorString := testSelectors[testName]
		expectedOutput := testOutput[testName]

		results, err := parser.GetNodes(selectorString)
		if err != nil {
			t.Error("Test ", testName, "failed: ", err)
			passed = false
		}

		//fmt.Println("Output")
		//for i, n := range results {
		//	fmt.Println(i, n.Type.Id.Value(), n.ValueString, n.Key, n.Null)
		//}

		if len(results) != len(expectedOutput) {
			t.Error("Test ", testName, " failed due to number of results being mismatched; ", len(results), " != ", len(expectedOutput), ": [Actual] ", results, " != [Expected] ", expectedOutput)
			passed = false
		} else {
			var expected = make([]interface{}, 0, 10)
			var actual = make([]*node.Node, 0, 10)
			//matchType := "string"

			for idx, result := range results {
				expectedEncoded := expectedOutput[idx]

				var expectedJson interface{}
				err := json.Unmarshal([]byte(expectedEncoded), &expectedJson)
				if err != nil {
					t.Error(
						"Test ", testName, " failed due to a JSON decoding error while decoding expectation: ", err,
					)
					passed = false
				}
				expected = append(expected, expectedJson)
				actual = append(actual, result)
			}

			// Iterate over each of the actual elements; if:
			// * We find a match, remove the match from the expected.
			// * We do not find a match, report an error
			for _, actualElement := range actual {
				var matched bool = false
				for expectedIdx, expectedElement := range expected {
					// TODO: Should we ignore the error here? I guess so...
					matched, _ = comparison(actualElement, expectedElement)
					if matched {
						expected = append(
							expected[:expectedIdx],
							expected[expectedIdx+1:]...,
						)
						break
					}
				}
				if !matched {
					t.Error("Actual element", actualElement, "not found in expected.")
					passed = false
					break
				}
			}
			if len(expected) > 0 {
				for _, value := range expected {
					t.Error("Expected element", value, "not found in actual.")
				}
				passed = false
			}
		}
		if passed {
			//t.Log("Test ", testName, " PASSED")
		} else {
			t.Error("Test ", testName, " FAILED")
		}
	}
}