Esempio n. 1
0
func TestSave(t *testing.T) {
	d, err := ioutil.TempDir("", "")
	require.NoError(t, err)
	defer os.RemoveAll(d)
	err = save(d, []byte{}, "b", false)
	require.NoError(t, err)
	if _, err := os.Stat(filepath.Join(d, "b")); err == nil {
		assert.Fail(t, "File should not exist")
	}

	// backup is on, but the file doesn't exist, so nothing to backup
	err = save(d, []byte("a"), "c", true)
	require.NoError(t, err)
	val, err := ioutil.ReadFile(filepath.Join(d, "c"))
	require.NoError(t, err)
	assert.Equal(t, "a", string(val))
	if _, err := os.Stat(filepath.Join(d, "c.backup")); err == nil {
		assert.Fail(t, "Backup file should not exist")
	}

	// backup is off, so the file is overwritten
	err = save(d, []byte("b"), "c", false)
	require.NoError(t, err)
	val, err = ioutil.ReadFile(filepath.Join(d, "c"))
	require.NoError(t, err)
	assert.Equal(t, "b", string(val))
	_, err = os.Stat(filepath.Join(d, "c.backup"))
	assert.Error(t, err)

	// backup is on and the file exists, so we should backup
	err = save(d, []byte("c"), "c", true)
	require.NoError(t, err)
	val, err = ioutil.ReadFile(filepath.Join(d, "c"))
	require.NoError(t, err)
	assert.Equal(t, "c", string(val))
	back, err := ioutil.ReadFile(filepath.Join(d, "c.backup"))
	require.NoError(t, err)
	assert.Equal(t, "b", string(back))

	// backup is on and the file exists, so we should backup and update the backup
	err = save(d, []byte("d"), "c", true)
	require.NoError(t, err)
	val, err = ioutil.ReadFile(filepath.Join(d, "c"))
	require.NoError(t, err)
	assert.Equal(t, "d", string(val))
	back, err = ioutil.ReadFile(filepath.Join(d, "c.backup"))
	require.NoError(t, err)
	assert.Equal(t, "c", string(back))

	// dir doesn't exist
	err = save(filepath.Join(d, "e"), []byte("f"), "g", true)
	require.NoError(t, err)
	val, err = ioutil.ReadFile(filepath.Join(d, "e", "g"))
	require.NoError(t, err)
	assert.Equal(t, "f", string(val))
}
Esempio n. 2
0
File: scan_test.go Progetto: kego/ke
func TestAll(t *testing.T) {
	dir, err := packages.GetDirFromPackage(context.Background(), "kego.io")
	require.NoError(t, err)

	// walk each file in the working directory
	walker := func(path string, file os.FileInfo, err error) error {
		// skip anything starting with a period, or not ending in .go.
		if strings.HasPrefix(path, ".") || !strings.HasSuffix(path, ".go") {
			return nil
		}
		return walkFile(path, t)
	}
	filepath.Walk(dir, walker)

	untested := map[string][]string{}
	for id, def := range all {
		if !def.thrown && (def.tested || def.skipped) {
			// Well, if the test isn't thrown the test should fail. Let's test
			// for it here anyway.
			assert.Fail(t, "Error tested but not thrown", id)
		}
		if def.thrown && def.new && !def.tested && !def.skipped {
			arr, ok := untested[def.pkg]
			if !ok {
				arr = []string{}
			}
			untested[def.pkg] = append(arr, id)
		}
	}

	//for pkg, def := range pkgs {
	//	if !def.tested && !def.notest {
	//assert.Fail(t, fmt.Sprintf("%s has no tests.", pkg))
	//	}
	//}

	if len(untested) > 0 {
		for pkg, tests := range untested {
			assert.Fail(t, fmt.Sprintf("Errors thrown in %s but not tested: %v", pkg, tests))
		}
	}
}
Esempio n. 3
0
func equal(t *testing.T, expected, actual *vecty.HTML) {
	assert.Equal(t, expected.TagName, actual.TagName)

	assert.Equal(t, len(expected.Properties), len(actual.Properties))
	for i, v := range expected.Properties {
		assert.Equal(t, v, actual.Properties[i])
	}

	assert.Equal(t, len(expected.Style), len(actual.Style))
	for i, v := range expected.Style {
		assert.Equal(t, v, actual.Style[i])
	}

	assert.Equal(t, len(expected.Dataset), len(actual.Dataset))
	for i, v := range expected.Dataset {
		assert.Equal(t, v, actual.Dataset[i])
	}

	assert.Equal(t, len(expected.EventListeners), len(actual.EventListeners))
	for i, e := range expected.EventListeners {
		a := actual.EventListeners[i]
		assert.Equal(t, e.Name, a.Name)
		// TODO: Compare events?
		//assert.Equal(t, e.Listener, a.Listener)
	}

	assert.Equal(t, len(expected.Children), len(actual.Children))
	for i, v := range expected.Children {
		if e, ok := v.(*vecty.HTML); ok {
			a, ok := actual.Children[i].(*vecty.HTML)
			if !ok {
				assert.Fail(t, "Should be *vecty.HTML")
			}
			equal(t, e, a)
		} else {
			expectedType := reflect.TypeOf(v)
			actualType := reflect.TypeOf(actual.Children[i])
			assert.Equal(t, expectedType, actualType)
		}
		// TODO: should we be comparing non *vecty.HTML?
	}
}
Esempio n. 4
0
File: scan_test.go Progetto: kego/ke
func (v *visitor) Visit(node ast.Node) (w ast.Visitor) {

	if node == nil {
		return v
	}

	switch ty := node.(type) {
	case *ast.File:
		for _, cg := range ty.Comments {
			for _, c := range cg.List {
				if strings.HasPrefix(c.Text, "// ke: ") {
					val := struct{ Package struct{ Notest bool } }{}
					err := json.Unmarshal([]byte(c.Text[7:]), &val)
					require.NoError(v.t, err)
					if val.Package.Notest {
						def := getPkgDef(v.pkg)
						def.notest = true
					}
				}
			}
		}
	case *ast.CallExpr:
		name := ""
		pkg := ""
		if f, ok := ty.Fun.(*ast.SelectorExpr); ok {
			name = f.Sel.Name
			if i, ok := f.X.(*ast.Ident); ok {
				pkg = i.Name
			}
			if pkg == "" {
				return v
			}
			if pkg == v.kerrName && (name == "New" || name == "Wrap") {
				def := getErrData(v.t, ty.Args, 0, v.filePath, v.fset.Position(f.Pos()))
				if def == nil {
					return v
				}
				if def.thrown {
					assert.Fail(v.t, fmt.Sprint("Duplicate kerr id: ", def.id))
				}
				def.pkg = v.pkg
				def.new = name == "New"
				def.thrown = true
			} else if (pkg == v.assertName || pkg == v.requireName) && (name == "IsError" || name == "HasError") {
				def := getErrData(v.t, ty.Args, 2, v.filePath, v.fset.Position(f.Pos()))
				if def == nil {
					return v
				}
				def.tested = true
			} else if (pkg == v.assertName || pkg == v.requireName) && name == "SkipError" {
				def := getErrData(v.t, ty.Args, 0, v.filePath, v.fset.Position(f.Pos()))
				if def == nil {
					return v
				}
				def.skipped = true
			}

		}
	}
	return v
}