226 lines
4.9 KiB
Go
Raw Normal View History

2016-12-15 12:03:02 +01:00
package goutils
import (
2017-05-18 18:54:23 +02:00
"fmt"
"testing"
2016-12-15 12:03:02 +01:00
)
// ****************************** TESTS ********************************************
func TestWrapNormalWord(t *testing.T) {
in := "Bob Manuel Bob Manuel"
out := "Bob Manuel\nBob Manuel"
wrapLength := 10
if x := Wrap(in, wrapLength); x != out {
t.Errorf("Wrap(%v) = %v, want %v", in, x, out)
}
}
func TestWrapCustomLongWordFalse(t *testing.T) {
in := "BobManuelBob Bob"
out := "BobManuelBob<br\\>Bob"
wrapLength := 10
newLineStr := "<br\\>"
wrapLongWords := false
if x := WrapCustom(in, wrapLength, newLineStr, wrapLongWords); x != out {
t.Errorf("Wrap(%v) = %v, want %v", in, x, out)
}
}
func TestWrapCustomLongWordTrue(t *testing.T) {
in := "BobManuelBob Bob"
out := "BobManuelB<br\\>ob Bob"
wrapLength := 10
newLineStr := "<br\\>"
wrapLongWords := true
if x := WrapCustom(in, wrapLength, newLineStr, wrapLongWords); x != out {
t.Errorf("WrapCustom(%v) = %v, want %v", in, x, out)
}
}
func TestCapitalize(t *testing.T) {
// Test 1: Checks if function works with 1 parameter, and default whitespace delimiter
2017-05-18 18:54:23 +02:00
in := "test is going.well.thank.you.for inquiring"
out := "Test Is Going.well.thank.you.for Inquiring"
2016-12-15 12:03:02 +01:00
if x := Capitalize(in); x != out {
t.Errorf("Capitalize(%v) = %v, want %v", in, x, out)
}
// Test 2: Checks if function works with both parameters, with param 2 containing whitespace and '.'
2017-05-18 18:54:23 +02:00
out = "Test Is Going.Well.Thank.You.For Inquiring"
delimiters := []rune{' ', '.'}
2016-12-15 12:03:02 +01:00
if x := Capitalize(in, delimiters...); x != out {
t.Errorf("Capitalize(%v) = %v, want %v", in, x, out)
}
}
func TestCapitalizeFully(t *testing.T) {
// Test 1
2017-05-18 18:54:23 +02:00
in := "tEsT iS goiNG.wELL.tHaNk.yOU.for inqUIrING"
out := "Test Is Going.well.thank.you.for Inquiring"
2016-12-15 12:03:02 +01:00
if x := CapitalizeFully(in); x != out {
t.Errorf("CapitalizeFully(%v) = %v, want %v", in, x, out)
}
// Test 2
2017-05-18 18:54:23 +02:00
out = "Test Is Going.Well.Thank.You.For Inquiring"
delimiters := []rune{' ', '.'}
2016-12-15 12:03:02 +01:00
if x := CapitalizeFully(in, delimiters...); x != out {
t.Errorf("CapitalizeFully(%v) = %v, want %v", in, x, out)
}
}
func TestUncapitalize(t *testing.T) {
// Test 1: Checks if function works with 1 parameter, and default whitespace delimiter
2017-05-18 18:54:23 +02:00
in := "This Is A.Test"
out := "this is a.Test"
2016-12-15 12:03:02 +01:00
if x := Uncapitalize(in); x != out {
t.Errorf("Uncapitalize(%v) = %v, want %v", in, x, out)
}
// Test 2: Checks if function works with both parameters, with param 2 containing whitespace and '.'
2017-05-18 18:54:23 +02:00
out = "this is a.test"
delimiters := []rune{' ', '.'}
2016-12-15 12:03:02 +01:00
if x := Uncapitalize(in, delimiters...); x != out {
t.Errorf("Uncapitalize(%v) = %v, want %v", in, x, out)
}
}
func TestSwapCase(t *testing.T) {
2017-05-18 18:54:23 +02:00
in := "This Is A.Test"
out := "tHIS iS a.tEST"
2016-12-15 12:03:02 +01:00
if x := SwapCase(in); x != out {
t.Errorf("SwapCase(%v) = %v, want %v", in, x, out)
}
}
func TestInitials(t *testing.T) {
// Test 1
2017-05-18 18:54:23 +02:00
in := "John Doe.Ray"
out := "JD"
2016-12-15 12:03:02 +01:00
if x := Initials(in); x != out {
t.Errorf("Initials(%v) = %v, want %v", in, x, out)
}
// Test 2
2017-05-18 18:54:23 +02:00
out = "JDR"
delimiters := []rune{' ', '.'}
2016-12-15 12:03:02 +01:00
if x := Initials(in, delimiters...); x != out {
t.Errorf("Initials(%v) = %v, want %v", in, x, out)
}
}
// ****************************** EXAMPLES ********************************************
func ExampleWrap() {
2017-05-18 18:54:23 +02:00
in := "Bob Manuel Bob Manuel"
2016-12-15 12:03:02 +01:00
wrapLength := 10
2017-05-18 18:54:23 +02:00
fmt.Println(Wrap(in, wrapLength))
// Output:
2016-12-15 12:03:02 +01:00
// Bob Manuel
// Bob Manuel
}
func ExampleWrapCustom_1() {
in := "BobManuelBob Bob"
wrapLength := 10
newLineStr := "<br\\>"
wrapLongWords := false
2017-05-18 18:54:23 +02:00
fmt.Println(WrapCustom(in, wrapLength, newLineStr, wrapLongWords))
// Output:
2016-12-15 12:03:02 +01:00
// BobManuelBob<br\>Bob
}
func ExampleWrapCustom_2() {
in := "BobManuelBob Bob"
wrapLength := 10
newLineStr := "<br\\>"
wrapLongWords := true
2017-05-18 18:54:23 +02:00
fmt.Println(WrapCustom(in, wrapLength, newLineStr, wrapLongWords))
// Output:
2016-12-15 12:03:02 +01:00
// BobManuelB<br\>ob Bob
}
func ExampleCapitalize() {
in := "test is going.well.thank.you.for inquiring" // Compare input to CapitalizeFully example
2017-05-18 18:54:23 +02:00
delimiters := []rune{' ', '.'}
2016-12-15 12:03:02 +01:00
2017-05-18 18:54:23 +02:00
fmt.Println(Capitalize(in))
fmt.Println(Capitalize(in, delimiters...))
2016-12-15 12:03:02 +01:00
// Output:
// Test Is Going.well.thank.you.for Inquiring
2017-05-18 18:54:23 +02:00
// Test Is Going.Well.Thank.You.For Inquiring
2016-12-15 12:03:02 +01:00
}
func ExampleCapitalizeFully() {
2017-05-18 18:54:23 +02:00
in := "tEsT iS goiNG.wELL.tHaNk.yOU.for inqUIrING" // Notice scattered capitalization
delimiters := []rune{' ', '.'}
2016-12-15 12:03:02 +01:00
2017-05-18 18:54:23 +02:00
fmt.Println(CapitalizeFully(in))
fmt.Println(CapitalizeFully(in, delimiters...))
2016-12-15 12:03:02 +01:00
// Output:
// Test Is Going.well.thank.you.for Inquiring
2017-05-18 18:54:23 +02:00
// Test Is Going.Well.Thank.You.For Inquiring
2016-12-15 12:03:02 +01:00
}
func ExampleUncapitalize() {
2017-05-18 18:54:23 +02:00
in := "This Is A.Test"
delimiters := []rune{' ', '.'}
2016-12-15 12:03:02 +01:00
2017-05-18 18:54:23 +02:00
fmt.Println(Uncapitalize(in))
fmt.Println(Uncapitalize(in, delimiters...))
2016-12-15 12:03:02 +01:00
// Output:
// this is a.Test
2017-05-18 18:54:23 +02:00
// this is a.test
2016-12-15 12:03:02 +01:00
}
func ExampleSwapCase() {
2017-05-18 18:54:23 +02:00
in := "This Is A.Test"
fmt.Println(SwapCase(in))
2016-12-15 12:03:02 +01:00
// Output:
// tHIS iS a.tEST
}
func ExampleInitials() {
2017-05-18 18:54:23 +02:00
in := "John Doe.Ray"
delimiters := []rune{' ', '.'}
2016-12-15 12:03:02 +01:00
2017-05-18 18:54:23 +02:00
fmt.Println(Initials(in))
fmt.Println(Initials(in, delimiters...))
2016-12-15 12:03:02 +01:00
// Output:
// JD
// JDR
}