gambuzzi

reader_test.go

May 11th, 2015
16
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package rpgkata
  2.  
  3. import (
  4.     "fmt"
  5.     "reflect"
  6.     "testing"
  7. )
  8.  
  9. func ObjectsAreEqual(expected, actual interface{}) bool {
  10.  
  11.     if expected == nil || actual == nil {
  12.         return expected == actual
  13.     }
  14.  
  15.     if reflect.DeepEqual(expected, actual) {
  16.         return true
  17.     }
  18.  
  19.     return false
  20.  
  21. }
  22.  
  23. // ObjectsAreEqualValues gets whether two objects are equal, or if their
  24. // values are equal.
  25. func ObjectsAreEqualValues(expected, actual interface{}) bool {
  26.     if ObjectsAreEqual(expected, actual) {
  27.         return true
  28.     }
  29.  
  30.     actualType := reflect.TypeOf(actual)
  31.     expectedValue := reflect.ValueOf(expected)
  32.     if expectedValue.Type().ConvertibleTo(actualType) {
  33.         // Attempt comparison after type conversion
  34.         if reflect.DeepEqual(actual, expectedValue.Convert(actualType).Interface()) {
  35.             return true
  36.         }
  37.     }
  38.  
  39.     return false
  40. }
  41.  
  42. func TestLine(t *testing.T) {
  43.     t.Parallel()
  44.     if testing.Short() {
  45.         t.Skip("skipping test in short mode.")
  46.     }
  47.  
  48.     actual := ReadLine("'Conan the Barber', 2, 10, 'Bastard Sword', 10")
  49.     excepted := new(StringOrArray)
  50.     excepted.Type = 2
  51.  
  52.   name := new(StringOrArray)
  53.   name.Type=1
  54.   name.Thestring = "'Conan the Barber'"
  55.   excepted.Thearray = append(excepted.Thearray, name)
  56.  
  57.   speed := new(StringOrArray)
  58.   speed.Type=1
  59.   speed.Thestring = "2"
  60.   excepted.Thearray = append(excepted.Thearray, speed)
  61.  
  62.   hp := new(StringOrArray)
  63.   hp.Type=1
  64.   hp.Thestring = "10"
  65.   excepted.Thearray = append(excepted.Thearray, hp)
  66.  
  67.   wn := new(StringOrArray)
  68.   wn.Type=1
  69.   wn.Thestring = "'Bastard Sword'"
  70.   excepted.Thearray = append(excepted.Thearray, wn)
  71.  
  72.   dm := new(StringOrArray)
  73.   dm.Type=1
  74.   dm.Thestring = "10"
  75.   excepted.Thearray = append(excepted.Thearray, dm)
  76.  
  77.     if !ObjectsAreEqual(excepted, actual) {
  78.         t.Errorf("ReadLine not working")
  79.     t.Fail()
  80.     }
  81.  
  82. }
RAW Paste Data