123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- package main
- import "testing"
- func TestProfileParse(t *testing.T) {
- // Setup the root node / test data
- // Already have parser tests so manually make them instead
- root := &Node{
- Line: "testing",
- } // This data is from Test Dummy.txt
- root.AddTag("root")
- n := root.NewChild()
- n.Set("pilot", "Test Dummy")
- n = root.NewChild()
- n.Set("date", "16 11 3013")
- n = root.NewChild()
- n.Set("system", "Rutilicus")
- n = root.NewChild()
- n.Set("planet", "New Boston")
- n = root.NewChild()
- n.Set("playtime", 19.816336)
- // Begin testing
- p := Profile{}
- err := p.Parse(root)
- if err != nil {
- t.Errorf("Failed parsing data, %v", err)
- }
- if p.Name != "Test Dummy" {
- t.Fail()
- t.Logf("Profile.Name should be 'Test Dummy', got '%s'", p.Name)
- }
- if p.Date != "16 11 3013" {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Profile.Date should be '16 11 3013', got '%s'", p.Date)
- }
- if p.Location.String() != "New Boston, Rutilicus" {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Profile.Location should be 'New Boston, Rutilicus', got '%s'", p.Location.String())
- }
- if p.Playtime != 19.816336 {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Profile.Playtime should be 19.816336, got %f", p.Playtime)
- }
- if p.PlayTime() != "19s" {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Profile.PlayTime() should be '19s', got '%s'", p.PlayTime())
- }
- }
- func TestProfile(t *testing.T) {
- p := Profile{}
- if p.Type() != "Profile" {
- t.Errorf("Profile didn't return correct type 'Profile', got '%s'", p.Type())
- }
- }
- func TestProfileUpdate(t *testing.T) {
- // Setup the root node / test data
- // Already have parser tests so manually make them instead
- root := &Node{
- Line: "testing",
- } // This data is from Test Dummy.txt
- root.AddTag("root")
- n := root.NewChild()
- n.Set("pilot", "Test Dummy")
- n = root.NewChild()
- n.Set("date", "16 11 3013")
- n = root.NewChild()
- n.Set("system", "Rutilicus")
- n = root.NewChild()
- n.Set("planet", "New Boston")
- n = root.NewChild()
- n.Set("playtime", 19.816336)
- // Begin testing
- p := Profile{}
- err := p.Parse(root)
- if err != nil {
- t.Fail()
- t.Logf("Profile.Parse failed, %v", err)
- }
- p.Playtime = 9.81
- err = p.Update()
- if err != nil {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Profile.Update failed, %v", err)
- }
- n1, err := root.Child(4)
- if err != nil {
- t.Errorf("Root.Child, %v", err)
- }
- if n1.Value() != "9.81" {
- t.Errorf("Profile.Update didn't change as expected, expected '9.81' as value, got '%s'", n1.Value())
- }
- }
- func TestProfileNonRoots(t *testing.T) {
- n := &Node{
- Line: "Hello World",
- }
- n.AddTag("test")
- p := Profile{}
- err := p.Parse(n)
- if err == nil {
- t.Fail()
- t.Logf("Expected error (non-root node), got none.")
- }
- err = p.Update()
- if err == nil {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Expected error (no node), got none.")
- }
- }
|