func TestInvalidWhenStringIsTooShort(t *testing.T) { spec := gspec.New(t) rule := MinLen(4) for _, str := range []string{"1", "12", "123"} { spec.Expect(rule.Verify(str)).ToEqual(false) } }
func TestInvalidWhenStringIsTooLong(t *testing.T) { spec := gspec.New(t) rule := MaxLen(4) for _, str := range []string{"12345", "123456"} { spec.Expect(rule.Verify(str)).ToEqual(false) } }
func TestParserReadsContainsCondition(t *testing.T) { spec := gspec.New(t) parser := newParser(" 'xyz' contains true%}") group, err := parser.ReadConditionGroup() spec.Expect(err).ToBeNil() assertParsedConditionGroup(t, group, "xyz", Contains, true) }
func TestSetsTheNotFoundResponse(t *testing.T) { spec := gspec.New(t) expected := Json("the res").Status(244).Response actual := Configure().NotFoundResponse(expected).notFound spec.Expect(actual.GetStatus()).ToEqual(244) spec.Expect(string(actual.GetBody())).ToEqual("the res") }
func TestReverseASingleElementArray(t *testing.T) { spec := gspec.New(t) filter := ReverseFactory(nil) values := filter([]bool{true}, nil).([]bool) spec.Expect(len(values)).ToEqual(1) spec.Expect(values[0]).ToEqual(true) }
func TestSetContainsAnExistingIdIfMultipleIndexesContainsIt(t *testing.T) { spec := gspec.New(t) union := NewUnion("x", []string{"apple", "orange"}) union.On(makeSetIndex(20, 23, 24, 25, 26)) union.On(makeSetIndex(20, 25, 28, 29)) spec.Expect(union.Contains(key.Type(20))).ToEqual(true) }
func TestValidWhenStringLengthIsWithinLen(t *testing.T) { spec := gspec.New(t) rule := Len(4, 6) for _, str := range []string{"1234", "12345", "123456"} { spec.Expect(rule.Verify(str)).ToEqual(true) } }
func TestMockIsLimitedToASingleInvocation(t *testing.T) { spec := gspec.New(t) fake := newFake() fake.Expect(fake.GetEmail).Returning("first").Once() spec.Expect(fake.GetEmail("leto")).ToEqual("first") spec.Expect(fake.GetEmail("paul")).ToEqual("*****@*****.**") }
func TestMockReturnsTheValueOnceByDefaultTimes(t *testing.T) { spec := gspec.New(t) fake := newFake() fake.Expect(fake.GetEmail).Returning("invalid") spec.Expect(fake.GetEmail("leto")).ToEqual("invalid") spec.Expect(fake.GetEmail("paul")).ToEqual("*****@*****.**") }
func assertStaticValue(t *testing.T, data string, expected interface{}) { spec := gspec.New(t) p := NewParser([]byte(data)) value, err := p.ReadValue() spec.Expect(err).ToBeNil() spec.Expect(value.Resolve(nil)).ToEqual(expected) }
func assertErrorValue(t *testing.T, data string, expected string) { spec := gspec.New(t) p := NewParser([]byte(data)) value, err := p.ReadValue() spec.Expect(err.Error()).ToEqual(expected) spec.Expect(value).ToBeNil() }
func TestStubReturnsTheValueMultipleTimes(t *testing.T) { spec := gspec.New(t) fake := newFake() fake.Stub(fake.GetEmail).Returning("invalid") spec.Expect(fake.GetEmail("leto")).ToEqual("invalid") spec.Expect(fake.GetEmail("paul")).ToEqual("invalid") }
func TestQueryCapsTheLimit(t *testing.T) { spec := gspec.New(t) db := SmallDB() defer db.Close() query := <-db.queryPool spec.Expect(query.Limit(200).(*NormalQuery).limit).ToEqual(100) }
func TestEmptyQueryOnInvalidIndex(t *testing.T) { spec := gspec.New(t) db := SmallDB() defer db.Close() _, ok := db.Query("cats").(*EmptyQuery) spec.Expect(ok).ToEqual(true) }
func TestParserReadsASinglePartial(t *testing.T) { spec := gspec.New(t) parser := newParser(" true %}") group, err := parser.ReadPartialCondition() spec.Expect(err).ToBeNil() assertParsedConditionGroup(t, group, true, UnknownComparator, nil) }
func TestIdMapReturnsAnExistingId(t *testing.T) { spec := gspec.New(t) m := newIdMap() m.get("over", true) m.get("9000", true) spec.Expect(m.get("over", false)).ToEqual(key.Type(1)) }
func TestParserReadsMultiplePartials(t *testing.T) { spec := gspec.New(t) parser := newParser(" 1 or 2%}") group, err := parser.ReadPartialCondition() spec.Expect(err).ToBeNil() assertParsedConditionGroup(t, group, 1, UnknownComparator, nil, OR, 2, UnknownComparator, nil) }
func TestIdMapRemovesAnId(t *testing.T) { spec := gspec.New(t) m := newIdMap() m.get("over", true) m.remove("over") spec.Expect(m.get("over", false)).ToEqual(key.NULL) }
func TestUnionDoesNotContainANonExistantId(t *testing.T) { spec := gspec.New(t) union := NewUnion("x", []string{"apple", "orange"}) union.On(makeSetIndex(20, 23, 24, 25, 26)) union.On(makeSetIndex(20, 25, 28, 29)) spec.Expect(union.Contains(key.Type(22))).ToEqual(false) }
func TestParserReadsAUnaryCondition(t *testing.T) { spec := gspec.New(t) parser := newParser(" true %}") group, err := parser.ReadConditionGroup() spec.Expect(err).ToBeNil() assertParsedConditionGroup(t, group, true, Unary, nil) }
func TestInvalidWhenStringLengthIsOutsideLen(t *testing.T) { spec := gspec.New(t) rule := Len(4, 6) for _, str := range []string{"123", "12", "1234567", "12345678"} { spec.Expect(rule.Verify(str)).ToEqual(false) } }
func TestParserReadsMultipleUnaryConditions(t *testing.T) { spec := gspec.New(t) parser := newParser(" true and false%}") group, err := parser.ReadConditionGroup() spec.Expect(err).ToBeNil() assertParsedConditionGroup(t, group, true, Unary, nil, AND, false, Unary, nil) }
func TestSetsTheBodyPoolSize(t *testing.T) { spec := gspec.New(t) c := Configure().BodyPool(10, 16) //allocate 1 extra byte so we know if the body is too large (or just right) spec.Expect(c.maxBodySize).ToEqual(int64(17)) spec.Expect(c.bodyPoolSize).ToEqual(10) }
func TestParserReadsSingleCondition(t *testing.T) { spec := gspec.New(t) parser := newParser(" true == 123 %}") group, err := parser.ReadConditionGroup() spec.Expect(err).ToBeNil() assertParsedConditionGroup(t, group, true, Equals, 123) }
func TestValidWhenStringIsShorterThanMaxLen(t *testing.T) { spec := gspec.New(t) rule := MaxLen(4) for _, str := range []string{"1", "12", "123", "1234"} { spec.Expect(rule.Verify(str)).ToEqual(true) } }
func TestParserReadsMultipleComplexConditions(t *testing.T) { spec := gspec.New(t) parser := newParser(" 'xyz' contains true or true and 123 > 445%}") group, err := parser.ReadConditionGroup() spec.Expect(err).ToBeNil() assertParsedConditionGroup(t, group, "xyz", Contains, true, OR, true, Unary, nil, AND, 123, GreaterThan, 445) }
func TestEmptyBackwards(t *testing.T) { spec := gspec.New(t) empty := NewEmpty("x") empty.Set(key.Type(4)) iter := empty.Backwards() spec.Expect(iter.Current()).ToEqual(key.NULL) }
func TestEmptyLength(t *testing.T) { spec := gspec.New(t) empty := NewEmpty("x") empty.Set(key.Type(4)) empty.Set(key.Type(6)) spec.Expect(empty.Len()).ToEqual(0) }
func TestValidWhenStringIsLongerThanMinLen(t *testing.T) { spec := gspec.New(t) rule := MinLen(4) for _, str := range []string{"1234", "12345", "123456"} { spec.Expect(rule.Verify(str)).ToEqual(true) } }
func TestSplitsAStringOnDefaultSpace(t *testing.T) { spec := gspec.New(t) filter := SplitFactory([]core.Value{}) values := filter("hello world", nil).([]string) spec.Expect(len(values)).ToEqual(2) spec.Expect(values[0]).ToEqual("hello") spec.Expect(values[1]).ToEqual("world") }