Parser (#9)
Reviewed-on: #9 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
package parser
|
||||
|
||||
import "testing"
|
||||
import "fmt"
|
||||
|
||||
const Test_File_Path = "../tests/numbers.txt"
|
||||
|
||||
// Add three tests
|
||||
// File dump
|
||||
func Test_FileDump(t *testing.T) {
|
||||
parser := NumberParser{FilePath: Test_File_Path}
|
||||
rawValues := parser.FileDump()
|
||||
|
||||
testValues := []SomeObject{
|
||||
{Value: "(217) 555-0194"},
|
||||
{Value: "(602) 555-0177"},
|
||||
{Value: "(713) 555-0167"},
|
||||
{Value: "(608) 555-0190"},
|
||||
{Value: "(305) 555-0112"},
|
||||
{Value: "(415) 555-0135"},
|
||||
{Value: "(608) 555-0190"},
|
||||
{Value: "(602) 555-0177"},
|
||||
{Value: "(317) 555-0123"},
|
||||
{Value: "(360) 555-0158"},
|
||||
}
|
||||
|
||||
if len(rawValues) < 1 {
|
||||
t.Error("Dump should not be empty")
|
||||
}
|
||||
|
||||
if !dataEqual(rawValues, testValues) {
|
||||
t.Error("File dump was not correctly dump")
|
||||
}
|
||||
}
|
||||
|
||||
// Parse values
|
||||
func Test_Parsed(t *testing.T) {
|
||||
parser := NumberParser{FilePath: Test_File_Path}
|
||||
rawValues := parser.FileDump()
|
||||
|
||||
if len(rawValues) < 1 {
|
||||
t.Error("Dump should not be empty")
|
||||
}
|
||||
|
||||
parsed := parser.UpdateValues(rawValues)
|
||||
testValues := []SomeObject{
|
||||
{Value: "+12175550194"},
|
||||
{Value: "+16025550177"},
|
||||
{Value: "+17135550167"},
|
||||
{Value: "+16085550190"},
|
||||
{Value: "+13055550112"},
|
||||
{Value: "+14155550135"},
|
||||
{Value: "+16085550190"},
|
||||
{Value: "+16025550177"},
|
||||
{Value: "+13175550123"},
|
||||
{Value: "+13605550158"},
|
||||
}
|
||||
|
||||
aSize := len(parsed)
|
||||
bSize := len(testValues)
|
||||
if aSize != bSize {
|
||||
t.Error("Data has varrying sizes A", aSize, " B", bSize)
|
||||
}
|
||||
if !dataEqual(parsed, testValues) {
|
||||
t.Error("Data has not be updated")
|
||||
}
|
||||
}
|
||||
|
||||
// Remove duplicates
|
||||
func Test_RemoveDups(t *testing.T) {
|
||||
parser := NumberParser{FilePath: Test_File_Path}
|
||||
rawValues := parser.FileDump()
|
||||
|
||||
if len(rawValues) < 1 {
|
||||
t.Error("Dump should not be empty")
|
||||
}
|
||||
|
||||
parsed := parser.UpdateValues(rawValues)
|
||||
finalAmount := parser.RemoveDups(parsed)
|
||||
testValues := []SomeObject{
|
||||
{Value: "+12175550194"},
|
||||
{Value: "+16025550177"},
|
||||
{Value: "+17135550167"},
|
||||
{Value: "+16085550190"},
|
||||
{Value: "+13055550112"},
|
||||
{Value: "+14155550135"},
|
||||
{Value: "+13175550123"},
|
||||
{Value: "+13605550158"},
|
||||
}
|
||||
|
||||
aSize := len(finalAmount)
|
||||
bSize := len(testValues)
|
||||
if aSize != bSize {
|
||||
t.Error("Data has varrying sizes A", aSize, " B", bSize)
|
||||
}
|
||||
if !dataEqual(finalAmount, testValues) {
|
||||
fmt.Println("Printing final amount")
|
||||
for _, val := range finalAmount {
|
||||
fmt.Println("Value:", val.Value)
|
||||
}
|
||||
fmt.Println("Printing test values")
|
||||
for _, val := range testValues {
|
||||
fmt.Println("Value:", val.Value)
|
||||
}
|
||||
|
||||
t.Error("Data has not be updated")
|
||||
}
|
||||
}
|
||||
|
||||
func dataEqual(a, b []SomeObject) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
|
||||
for i := range a {
|
||||
foundIt := false
|
||||
for j := range b {
|
||||
if a[i] == b[j] {
|
||||
foundIt = true
|
||||
}
|
||||
}
|
||||
|
||||
if !foundIt {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user