profile_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package main
  2. import "testing"
  3. func TestProfileParse(t *testing.T) {
  4. // Setup the root node / test data
  5. // Already have parser tests so manually make them instead
  6. root := &Node{
  7. Line: "testing",
  8. } // This data is from Test Dummy.txt
  9. root.AddTag("root")
  10. n := root.NewChild()
  11. n.Set("pilot", "Test Dummy")
  12. n = root.NewChild()
  13. n.Set("date", "16 11 3013")
  14. n = root.NewChild()
  15. n.Set("system", "Rutilicus")
  16. n = root.NewChild()
  17. n.Set("planet", "New Boston")
  18. n = root.NewChild()
  19. n.Set("playtime", 19.816336)
  20. // Begin testing
  21. p := Profile{}
  22. err := p.Parse(root)
  23. if err != nil {
  24. t.Errorf("Failed parsing data, %v", err)
  25. }
  26. if p.Name != "Test Dummy" {
  27. t.Fail()
  28. t.Logf("Profile.Name should be 'Test Dummy', got '%s'", p.Name)
  29. }
  30. if p.Date != "16 11 3013" {
  31. if !t.Failed() {
  32. t.Fail()
  33. }
  34. t.Logf("Profile.Date should be '16 11 3013', got '%s'", p.Date)
  35. }
  36. if p.Location.String() != "New Boston, Rutilicus" {
  37. if !t.Failed() {
  38. t.Fail()
  39. }
  40. t.Logf("Profile.Location should be 'New Boston, Rutilicus', got '%s'", p.Location.String())
  41. }
  42. if p.Playtime != 19.816336 {
  43. if !t.Failed() {
  44. t.Fail()
  45. }
  46. t.Logf("Profile.Playtime should be 19.816336, got %f", p.Playtime)
  47. }
  48. if p.PlayTime() != "19s" {
  49. if !t.Failed() {
  50. t.Fail()
  51. }
  52. t.Logf("Profile.PlayTime() should be '19s', got '%s'", p.PlayTime())
  53. }
  54. }
  55. func TestProfile(t *testing.T) {
  56. p := Profile{}
  57. if p.Type() != "Profile" {
  58. t.Errorf("Profile didn't return correct type 'Profile', got '%s'", p.Type())
  59. }
  60. }
  61. func TestProfileUpdate(t *testing.T) {
  62. // Setup the root node / test data
  63. // Already have parser tests so manually make them instead
  64. root := &Node{
  65. Line: "testing",
  66. } // This data is from Test Dummy.txt
  67. root.AddTag("root")
  68. n := root.NewChild()
  69. n.Set("pilot", "Test Dummy")
  70. n = root.NewChild()
  71. n.Set("date", "16 11 3013")
  72. n = root.NewChild()
  73. n.Set("system", "Rutilicus")
  74. n = root.NewChild()
  75. n.Set("planet", "New Boston")
  76. n = root.NewChild()
  77. n.Set("playtime", 19.816336)
  78. // Begin testing
  79. p := Profile{}
  80. err := p.Parse(root)
  81. if err != nil {
  82. t.Fail()
  83. t.Logf("Profile.Parse failed, %v", err)
  84. }
  85. p.Playtime = 9.81
  86. err = p.Update()
  87. if err != nil {
  88. if !t.Failed() {
  89. t.Fail()
  90. }
  91. t.Logf("Profile.Update failed, %v", err)
  92. }
  93. n1, err := root.Child(4)
  94. if err != nil {
  95. t.Errorf("Root.Child, %v", err)
  96. }
  97. if n1.Value() != "9.81" {
  98. t.Errorf("Profile.Update didn't change as expected, expected '9.81' as value, got '%s'", n1.Value())
  99. }
  100. }
  101. func TestProfileNonRoots(t *testing.T) {
  102. n := &Node{
  103. Line: "Hello World",
  104. }
  105. n.AddTag("test")
  106. p := Profile{}
  107. err := p.Parse(n)
  108. if err == nil {
  109. t.Fail()
  110. t.Logf("Expected error (non-root node), got none.")
  111. }
  112. err = p.Update()
  113. if err == nil {
  114. if !t.Failed() {
  115. t.Fail()
  116. }
  117. t.Logf("Expected error (no node), got none.")
  118. }
  119. }