Example #1
0
func TestRouteMatchFails(t *testing.T) {
	var assertion tameshigiri.Assertion
	var givenPattern string
	var newRoute *Route

	assertion = tameshigiri.NewAssertion(t)

	givenPattern = "/user/<alias>"
	newRoute = NewRoute(givenPattern, true)

	matches := newRoute.Match("/user/shiroyuki/asdf")

	assertion.IsTrue(matches == nil, "There should not be a match.")
}
Example #2
0
func TestWebRoutingNonReversibleRouteGetCompiledPattern(t *testing.T) {
	var assertion tameshigiri.Assertion
	var givenPattern string
	var newRoute *Route

	assertion = tameshigiri.NewAssertion(t)

	givenPattern = "/user/(?P<alias>[^/]+)"
	newRoute = NewRoute(givenPattern, false)

	compiled, err := newRoute.GetCompiledPattern()

	assertion.IsTrue(compiled != nil, "Unexpected compiled pattern")
	assertion.IsTrue(err == nil, "No error raised")
}
Example #3
0
func TestWebRoutingReversibleRouteGetCompiledPatternFailed(t *testing.T) {
	var assertion tameshigiri.Assertion
	var givenPattern string
	var newRoute *Route

	assertion = tameshigiri.NewAssertion(t)

	givenPattern = "/user/<alias>/<alias>"
	newRoute = NewRoute(givenPattern, true)

	compiled, err := newRoute.GetCompiledPattern()

	assertion.IsTrue(compiled == nil, "The pattern should not be compiled.")
	assertion.IsTrue(err != nil, "An error should be raised.")
}
Example #4
0
func TestRouteMatchOk(t *testing.T) {
	var assertion tameshigiri.Assertion
	var givenPattern string
	var newRoute *Route

	assertion = tameshigiri.NewAssertion(t)

	givenPattern = "/user/<alias>"
	newRoute = NewRoute(givenPattern, true)

	matches := newRoute.Match("/user/shiroyuki")

	assertion.IsTrue(matches != nil, "There should be a match.")

	actual := (*matches.Key("alias"))[0]

	assertion.Equals("shiroyuki", actual, "This should be 'shiroyuki'.")
}
Example #5
0
func TestRecordListAppendOneRecordAndFindWithNoResult(t *testing.T) {
	assertion := tu.NewAssertion(t)
	recordList := RecordList{DebugMode: true}

	head := &Record{
		Method: METHOD_GET,
		Route:  NewRoute("/a/<k1>/<k2>", true),
	}

	assertion.IsFalse(recordList.HasAny(), "This list should not have the head record.")

	recordList.Append(head)

	assertion.IsTrue(recordList.HasAny(), "This list should have the head record.")

	r, m := recordList.Find(METHOD_GET, "/a/b")

	assertion.IsTrue(r == nil && m == nil, "There should be no matches.")
}
Example #6
0
func localTestWebRoutingRouteNewRoute(t *testing.T, givenPattern string, reversible bool) {
	var assertion tameshigiri.Assertion
	var newRoute *Route

	assertion = tameshigiri.NewAssertion(t)
	newRoute = NewRoute(givenPattern, reversible)

	assertion.IsTrue(
		newRoute.Pattern == givenPattern,
		"The registered pattern must be exactly the same as the given one.",
	)

	assertion.IsTrue(
		newRoute.Reversible == reversible,
		"The registered pattern must be reversible.",
	)

	assertion.IsTrue(
		newRoute.RePattern == nil,
		"The compiled pattern should not be compiled during initialization.",
	)
}
Example #7
0
func TestRecordListInitial(t *testing.T) {
	assertion := tu.NewAssertion(t)
	recordList := RecordList{}

	assertion.IsFalse(recordList.HasAny(), "This list should have not had the head record.")
}
Example #8
0
func localTestReSearchAll(
	t *testing.T,
	pattern string,
	sample string,
	expectedList []string,
	expectedDict map[string][]string,
) {
	var index int
	var key string
	var actualL *string
	var expected string

	var actuals *[]string
	var expecteds []string

	var assertion = tameshigiri.NewAssertion(t)
	var compiled = Compile(pattern)
	var result = compiled.SearchAll(sample)

	//fmt.Println(result.Dictionary)
	//fmt.Println(result.ItemList)

	var expectedListLength = len(expectedList)
	var expectedDictLength = len(expectedDict)
	var expectedTotalCount = expectedListLength + expectedDictLength

	if expectedTotalCount > 0 {
		assertion.IsTrue(result.HasAny(), "This should be found.")
	}

	assertion.Equals(
		expectedTotalCount,
		result.Count(),
		"Total count",
	)

	assertion.Equals(
		expectedListLength,
		result.CountIndices(),
		"Item-list count",
	)

	if expectedListLength > 0 {
		for index = range expectedList {
			actualL = result.Index(index)
			expected = expectedList[index]

			assertion.Equals(
				expected,
				*actualL,
				fmt.Sprintf("List#%d", index),
			)
		}
	}

	assertion.Equals(
		expectedDictLength,
		result.CountKeys(),
		"Dictionary count",
	)

	if expectedDictLength > 0 {
		for key, expecteds = range expectedDict {
			actuals = result.Key(key)

			for index = range expecteds {
				assertion.Equals(
					expecteds[index],
					(*actuals)[index],
					fmt.Sprintf("Dictionary[%s][%d]", key, index),
				)
			}
		}
	}
}