add some tests
Former-commit-id: c9c397710ea65ce273ebbe584276ef5eff7d2ca1 [formerly cef3eaec1126e45e2e64225c2224ab313954dbc4] [formerly 550903ae12bf3d907f67572bb5c204a0079bf42f [formerly 1f52d5d0fda0d1132d04c900565f2ecffccfd778]] Former-commit-id: a66194a23a57f764a76b4f7959812257331e19db [formerly 3827a7330f021001aa2af61678e4ee9219e2571a] Former-commit-id: 5a6fd2a47a07e32e1ea58e05c8b4493be38bb398
This commit is contained in:
		
							parent
							
								
									589f790ef7
								
							
						
					
					
						commit
						52fd0fbefd
					
				| 
						 | 
				
			
			@ -76,7 +76,7 @@ func Unmarshal(content []byte) (interface{}, error) {
 | 
			
		|||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
	default:
 | 
			
		||||
		return nil, errors.New("Invalid frontmatter type.")
 | 
			
		||||
		return nil, errors.New("Invalid frontmatter type")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return data, nil
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -36,10 +36,10 @@ func RuneToStringFormat(mark rune) (string, error) {
 | 
			
		|||
		return "yaml", nil
 | 
			
		||||
	case '+':
 | 
			
		||||
		return "toml", nil
 | 
			
		||||
	case '{':
 | 
			
		||||
	case '{', '}':
 | 
			
		||||
		return "json", nil
 | 
			
		||||
	default:
 | 
			
		||||
		return "", errors.New("Unsupported format type.")
 | 
			
		||||
		return "", errors.New("Unsupported format type")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -53,6 +53,6 @@ func StringFormatToRune(format string) (rune, error) {
 | 
			
		|||
	case "json":
 | 
			
		||||
		return '{', nil
 | 
			
		||||
	default:
 | 
			
		||||
		return '0', errors.New("Unsupported format type.")
 | 
			
		||||
		return '0', errors.New("Unsupported format type")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,131 @@
 | 
			
		|||
package frontmatter
 | 
			
		||||
 | 
			
		||||
import "testing"
 | 
			
		||||
 | 
			
		||||
type hasRuneTest struct {
 | 
			
		||||
	File   []byte
 | 
			
		||||
	Return bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var testHasRune = []hasRuneTest{
 | 
			
		||||
	hasRuneTest{
 | 
			
		||||
		File: []byte(`---
 | 
			
		||||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
 | 
			
		||||
Sed auctor libero eget ante fermentum commodo. 
 | 
			
		||||
---`),
 | 
			
		||||
		Return: true,
 | 
			
		||||
	},
 | 
			
		||||
	hasRuneTest{
 | 
			
		||||
		File: []byte(`+++
 | 
			
		||||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
 | 
			
		||||
Sed auctor libero eget ante fermentum commodo. 
 | 
			
		||||
+++`),
 | 
			
		||||
		Return: true,
 | 
			
		||||
	},
 | 
			
		||||
	hasRuneTest{
 | 
			
		||||
		File: []byte(`{
 | 
			
		||||
	"json": "Lorem ipsum dolor sit amet"
 | 
			
		||||
}`),
 | 
			
		||||
		Return: true,
 | 
			
		||||
	},
 | 
			
		||||
	hasRuneTest{
 | 
			
		||||
		File:   []byte(`+`),
 | 
			
		||||
		Return: false,
 | 
			
		||||
	},
 | 
			
		||||
	hasRuneTest{
 | 
			
		||||
		File:   []byte(`++`),
 | 
			
		||||
		Return: false,
 | 
			
		||||
	},
 | 
			
		||||
	hasRuneTest{
 | 
			
		||||
		File:   []byte(`-`),
 | 
			
		||||
		Return: false,
 | 
			
		||||
	},
 | 
			
		||||
	hasRuneTest{
 | 
			
		||||
		File:   []byte(`--`),
 | 
			
		||||
		Return: false,
 | 
			
		||||
	},
 | 
			
		||||
	hasRuneTest{
 | 
			
		||||
		File:   []byte(`Lorem ipsum`),
 | 
			
		||||
		Return: false,
 | 
			
		||||
	},
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestHasRune(t *testing.T) {
 | 
			
		||||
	for _, test := range testHasRune {
 | 
			
		||||
		if HasRune(test.File) != test.Return {
 | 
			
		||||
			t.Error("Incorrect value on HasRune")
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type appendRuneTest struct {
 | 
			
		||||
	Before []byte
 | 
			
		||||
	After  []byte
 | 
			
		||||
	Mark   rune
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var testAppendRuneTest = []appendRuneTest{}
 | 
			
		||||
 | 
			
		||||
func TestAppendRune(t *testing.T) {
 | 
			
		||||
	for i, test := range testAppendRuneTest {
 | 
			
		||||
		if !compareByte(AppendRune(test.Before, test.Mark), test.After) {
 | 
			
		||||
			t.Errorf("Incorrect value on AppendRune of Test %d", i)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func compareByte(a, b []byte) bool {
 | 
			
		||||
	if a == nil && b == nil {
 | 
			
		||||
		return true
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if a == nil || b == nil {
 | 
			
		||||
		return false
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(a) != len(b) {
 | 
			
		||||
		return false
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for i := range a {
 | 
			
		||||
		if a[i] != b[i] {
 | 
			
		||||
			return false
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var testRuneToStringFormat = map[rune]string{
 | 
			
		||||
	'-': "yaml",
 | 
			
		||||
	'+': "toml",
 | 
			
		||||
	'{': "json",
 | 
			
		||||
	'}': "json",
 | 
			
		||||
	'1': "",
 | 
			
		||||
	'a': "",
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestRuneToStringFormat(t *testing.T) {
 | 
			
		||||
	for mark, format := range testRuneToStringFormat {
 | 
			
		||||
		val, _ := RuneToStringFormat(mark)
 | 
			
		||||
		if val != format {
 | 
			
		||||
			t.Errorf("Incorrect value on RuneToStringFormat of %v; want: %s; got: %s", mark, format, val)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var testStringFormatToRune = map[string]rune{
 | 
			
		||||
	"yaml":  '-',
 | 
			
		||||
	"toml":  '+',
 | 
			
		||||
	"json":  '{',
 | 
			
		||||
	"lorem": '0',
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestStringFormatToRune(t *testing.T) {
 | 
			
		||||
	for format, mark := range testStringFormatToRune {
 | 
			
		||||
		val, _ := StringFormatToRune(format)
 | 
			
		||||
		if val != mark {
 | 
			
		||||
			t.Errorf("Incorrect value on StringFormatToRune of %s; want: %v; got: %v", format, mark, val)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,49 @@
 | 
			
		|||
package variables
 | 
			
		||||
 | 
			
		||||
import "testing"
 | 
			
		||||
 | 
			
		||||
type interfaceToBool struct {
 | 
			
		||||
	Value  interface{}
 | 
			
		||||
	Result bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var testIsMap = []*interfaceToBool{
 | 
			
		||||
	&interfaceToBool{"teste", false},
 | 
			
		||||
	&interfaceToBool{453478, false},
 | 
			
		||||
	&interfaceToBool{-984512, false},
 | 
			
		||||
	&interfaceToBool{true, false},
 | 
			
		||||
	&interfaceToBool{map[string]bool{}, true},
 | 
			
		||||
	&interfaceToBool{map[int]bool{}, true},
 | 
			
		||||
	&interfaceToBool{map[interface{}]bool{}, true},
 | 
			
		||||
	&interfaceToBool{[]string{}, false},
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestIsMap(t *testing.T) {
 | 
			
		||||
	for _, test := range testIsMap {
 | 
			
		||||
		if IsMap(test.Value) != test.Result {
 | 
			
		||||
			t.Errorf("Incorrect value on IsMap for %v; want: %v; got: %v", test.Value, test.Result, !test.Result)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var testIsSlice = []*interfaceToBool{
 | 
			
		||||
	&interfaceToBool{"teste", false},
 | 
			
		||||
	&interfaceToBool{453478, false},
 | 
			
		||||
	&interfaceToBool{-984512, false},
 | 
			
		||||
	&interfaceToBool{true, false},
 | 
			
		||||
	&interfaceToBool{map[string]bool{}, false},
 | 
			
		||||
	&interfaceToBool{map[int]bool{}, false},
 | 
			
		||||
	&interfaceToBool{map[interface{}]bool{}, false},
 | 
			
		||||
	&interfaceToBool{[]string{}, true},
 | 
			
		||||
	&interfaceToBool{[]int{}, true},
 | 
			
		||||
	&interfaceToBool{[]bool{}, true},
 | 
			
		||||
	&interfaceToBool{[]interface{}{}, true},
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestIsSlice(t *testing.T) {
 | 
			
		||||
	for _, test := range testIsSlice {
 | 
			
		||||
		if IsSlice(test.Value) != test.Result {
 | 
			
		||||
			t.Errorf("Incorrect value on IsSlice for %v; want: %v; got: %v", test.Value, test.Result, !test.Result)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue