Beispiel #1
0
func TestAddMutationRoot(t *testing.T) {
	cb, _ := data.Setup(t)
	a := node.NewNode()
	ty, ok := system.GetTypeFromCache(cb.Ctx(), "kego.io/tests/data", "multi")
	require.True(t, ok)
	require.NoError(t, mutateAddNode(cb.Ctx(), a, nil, "", 2, ty, "z"))
	require.NotNil(t, a.Value.(*data.Multi).Id)
	require.Equal(t, "z", a.Value.(*data.Multi).Id.Name)
}
Beispiel #2
0
func TestNewView(t *testing.T) {
	m := gomock.NewController(t)
	defer m.Finish()
	ctx := context.Background()
	app := mock_flux.NewMockAppInterface(m)
	v := NewTestView(ctx, app, false)
	require.NotNil(t, v.Composite)
	v.Body = vecty.Nil()
	require.IsType(t, elem.Div(), v.RenderFunc())
	v.Unmount()
	require.IsType(t, vecty.Nil(), v.RenderFunc())
}
Beispiel #3
0
func TestAddMutationRedo(t *testing.T) {
	cb, n := data.Setup(t)

	test := func(t *testing.T, n *node.Node, m *data.Multi) {

		var a, p, b, a1, p1, b1 *node.Node
		a = node.NewNode()
		p = n.Map["am"]
		b = node.NewNode()
		ty, ok := system.GetTypeFromCache(cb.Ctx(), "kego.io/tests/data", "multi")
		require.True(t, ok)
		require.NoError(t, mutateAddNode(cb.Ctx(), a, p, "", 2, ty, ""))
		require.Equal(t, `[{"js":"amjs0","type":"multi"},{"js":"amjs1","type":"multi"},{"type":"multi"}]`, p.Print(cb.Ctx()))

		a1 = n.Map["am"].Array[2].Map["m"]
		p1 = n.Map["am"].Array[2]
		b1 = node.NewNode()
		require.NoError(t, mutateAddNode(cb.Ctx(), a1, p1, "m", -1, ty, ""))
		require.Equal(t, 3, len(n.Map["am"].Array))
		require.Equal(t, 3, len(m.Am))
		require.False(t, n.Map["am"].Array[2].Map["m"].Missing)
		require.Equal(t, `[{"js":"amjs0","type":"multi"},{"js":"amjs1","type":"multi"},{"m":{"type":"multi"},"type":"multi"}]`, p.Print(cb.Ctx()))

		require.NoError(t, mutateDeleteNode(cb.Ctx(), a, p, b))
		require.NoError(t, mutateDeleteNode(cb.Ctx(), a1, p1, b1))
		require.Equal(t, 2, len(n.Map["am"].Array))
		require.Equal(t, 2, len(m.Am))
		require.NoError(t, mutateRestoreNode(cb.Ctx(), a, p, b))
		require.NoError(t, mutateRestoreNode(cb.Ctx(), a1, p1, b1))
		require.Equal(t, 3, len(n.Map["am"].Array))
		require.Equal(t, 3, len(m.Am))
		require.NotNil(t, n.Map["am"].Array[2])
		require.NotNil(t, m.Am[2])
	}

	test(t, n.Map["m"], n.Value.(*data.Multi).M)
}
Beispiel #4
0
func TestDeleteMutation(t *testing.T) {

	cb, n := data.Setup(t)

	test := func(t *testing.T, n *node.Node, m *data.Multi) {
		var b, d, p *node.Node

		b = node.NewNode()
		d = n.Map["jb"]
		p = n
		require.NoError(t, mutateDeleteNode(cb.Ctx(), d, p, b))
		assert.False(t, n.Map["jb"].ValueBool)
		assert.False(t, m.Jb)
		require.NoError(t, mutateRestoreNode(cb.Ctx(), d, p, b))
		assert.True(t, n.Map["jb"].ValueBool)
		assert.True(t, m.Jb)

		b = node.NewNode()
		d = n.Map["ss"]
		p = n
		require.NoError(t, mutateDeleteNode(cb.Ctx(), d, p, b))
		assert.True(t, n.Map["ss"].Missing)
		assert.Equal(t, "", n.Map["ss"].ValueString)
		assert.Nil(t, m.Ss)
		require.NoError(t, mutateRestoreNode(cb.Ctx(), d, p, b))
		assert.False(t, n.Map["ss"].Missing)
		assert.Equal(t, "ss1", n.Map["ss"].ValueString)
		require.NotNil(t, m.Ss)
		assert.Equal(t, "ss1", m.Ss.Value())

		b = node.NewNode()
		d = n.Map["i"]
		p = n
		require.NoError(t, mutateDeleteNode(cb.Ctx(), d, p, b))
		assert.True(t, n.Map["i"].Missing)
		assert.Nil(t, m.I)
		require.NoError(t, mutateRestoreNode(cb.Ctx(), d, p, b))
		assert.False(t, n.Map["i"].Missing)
		assert.Equal(t, "ia", n.Map["i"].Value.(*data.Facea).A.Value())
		require.NotNil(t, m.I)
		assert.Equal(t, "ia", m.I.Face())

		b = node.NewNode()
		d = n.Map["ass"]
		p = n
		require.NoError(t, mutateDeleteNode(cb.Ctx(), d, p, b))
		assert.True(t, n.Map["ass"].Missing)
		assert.Equal(t, 0, len(m.Ass))
		require.NoError(t, mutateRestoreNode(cb.Ctx(), d, p, b))
		assert.False(t, n.Map["ass"].Missing)
		assert.Equal(t, 4, len(m.Ass))

		b = node.NewNode()
		d = n.Map["mss"]
		p = n
		require.NoError(t, mutateDeleteNode(cb.Ctx(), d, p, b))
		assert.True(t, n.Map["mss"].Missing)
		assert.Nil(t, m.Mss)
		require.NoError(t, mutateRestoreNode(cb.Ctx(), d, p, b))
		assert.False(t, n.Map["mss"].Missing)
		require.NotNil(t, m.Mss)
		assert.Equal(t, 2, len(m.Mss))
		require.NotNil(t, m.Mss["a"])
		assert.Equal(t, "mssa", m.Mss["a"].Value())

		b = node.NewNode()
		d = n.Map["ass"].Array[0]
		p = n.Map["ass"]
		require.NoError(t, mutateDeleteNode(cb.Ctx(), d, p, b))
		assert.Equal(t, 3, len(n.Map["ass"].Array))
		assert.Equal(t, 3, len(m.Ass))
		require.NoError(t, mutateRestoreNode(cb.Ctx(), d, p, b))
		assert.Equal(t, 4, len(n.Map["ass"].Array))
		assert.Equal(t, 4, len(m.Ass))
		assert.Equal(t, "ass0", n.Map["ass"].Array[0].ValueString)
		require.NotNil(t, m.Ass[0])
		assert.Equal(t, "ass0", m.Ass[0].Value())

	}

	data.Run(t, n, n.Value.(*data.Multi), test)
}
Beispiel #5
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")
		}
	}
}