github.com/hzck/speedroute@v0.0.0-20201115191102-403b7d0e443f/parser/livesplitdataparser_test.go (about)

     1  package parser
     2  
     3  import (
     4  	"encoding/json"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  const data16 string = `
    10  <?xml version="1.0" encoding="UTF-8"?>
    11  <Run version="1.6.0">
    12    <Segments>
    13      <Segment>
    14        <Name>This is 1</Name>
    15        <BestSegmentTime>
    16          <RealTime>00:00:04.9200000</RealTime>
    17        </BestSegmentTime>
    18      </Segment>
    19      <Segment>
    20        <Name>2 fo' sho'</Name>
    21        <BestSegmentTime>
    22          <RealTime>00:00:04.6610000</RealTime>
    23        </BestSegmentTime>
    24      </Segment>
    25      <Segment>
    26        <Name>Segment 3</Name>
    27        <BestSegmentTime>
    28          <RealTime>00:00:02.9120000</RealTime>
    29        </BestSegmentTime>
    30      </Segment>
    31      <Segment>
    32        <Name>4</Name>
    33        <BestSegmentTime>
    34          <RealTime>00:00:25.3680000</RealTime>
    35        </BestSegmentTime>
    36      </Segment>
    37      <Segment>
    38        <Name>5</Name>
    39        <BestSegmentTime>
    40          <RealTime>12:20:01.6740000</RealTime>
    41        </BestSegmentTime>
    42      </Segment>
    43      <Segment>
    44        <Name>6</Name>
    45        <BestSegmentTime>
    46          <RealTime>02:02:03.4480000</RealTime>
    47        </BestSegmentTime>
    48      </Segment>
    49      <Segment>
    50        <Name>7</Name>
    51        <BestSegmentTime>
    52          <RealTime>00:00:00.3525000</RealTime>
    53        </BestSegmentTime>
    54      </Segment>
    55      <Segment>
    56        <Name>8 going to the end</Name>
    57        <BestSegmentTime>
    58          <RealTime>00:00:03.7524000</RealTime>
    59        </BestSegmentTime>
    60      </Segment>
    61    </Segments>
    62  </Run>
    63  `
    64  
    65  const answer16 string = `
    66  {
    67    "rewards": [],
    68    "nodes": [
    69      {
    70        "id": "START",
    71        "rewards": []
    72      },
    73      {
    74        "id": "This is 1",
    75        "rewards": []
    76      },
    77      {
    78        "id": "2 fo' sho'",
    79        "rewards": []
    80      },
    81      {
    82        "id": "Segment 3",
    83        "rewards": []
    84      },
    85      {
    86        "id": "4",
    87        "rewards": []
    88      },
    89      {
    90        "id": "5",
    91        "rewards": []
    92      },
    93      {
    94        "id": "6",
    95        "rewards": []
    96      },
    97      {
    98        "id": "7",
    99        "rewards": []
   100      },
   101      {
   102        "id": "8 going to the end",
   103        "rewards": []
   104      }
   105    ],
   106    "edges": [
   107      {
   108        "from": "START",
   109        "to": "This is 1",
   110        "weights": [
   111          {
   112            "time": "00:00:04.9200000",
   113            "requirements": []
   114          }
   115        ]
   116      },
   117      {
   118        "from": "This is 1",
   119        "to": "2 fo' sho'",
   120        "weights": [
   121          {
   122            "time": "00:00:04.6610000",
   123            "requirements": []
   124          }
   125        ]
   126      },
   127      {
   128        "from": "2 fo' sho'",
   129        "to": "Segment 3",
   130        "weights": [
   131          {
   132            "time": "00:00:02.9120000",
   133            "requirements": []
   134          }
   135        ]
   136      },
   137      {
   138        "from": "Segment 3",
   139        "to": "4",
   140        "weights": [
   141          {
   142            "time": "00:00:25.3680000",
   143            "requirements": []
   144          }
   145        ]
   146      },
   147      {
   148        "from": "4",
   149        "to": "5",
   150        "weights": [
   151          {
   152            "time": "12:20:01.6740000",
   153            "requirements": []
   154          }
   155        ]
   156      },
   157      {
   158        "from": "5",
   159        "to": "6",
   160        "weights": [
   161          {
   162            "time": "02:02:03.4480000",
   163            "requirements": []
   164          }
   165        ]
   166      },
   167      {
   168        "from": "6",
   169        "to": "7",
   170        "weights": [
   171          {
   172            "time": "00:00:00.3525000",
   173            "requirements": []
   174          }
   175        ]
   176      },
   177      {
   178        "from": "7",
   179        "to": "8 going to the end",
   180        "weights": [
   181          {
   182            "time": "00:00:03.7524000",
   183            "requirements": []
   184          }
   185        ]
   186      }
   187    ],
   188    "startId": "START",
   189    "endId": "8 going to the end"
   190  }
   191  `
   192  
   193  // TestParseVersion16 tests that version livesplit data version 1.6 is parseable.
   194  func TestParseVersion16(t *testing.T) {
   195  	var xmljson, correct graph
   196  	result, err := LivesplitXMLtoJSON(data16)
   197  	validateError(t, err)
   198  	validateError(t, json.Unmarshal([]byte(result), &xmljson))
   199  	validateError(t, json.Unmarshal([]byte(answer16), &correct))
   200  	if !reflect.DeepEqual(xmljson, correct) {
   201  		t.Errorf("The parsed result is not correct:\n" + result + "\nVS\n" + answer16)
   202  	}
   203  	t.Log(xmljson)
   204  	t.Log(correct)
   205  }
   206  
   207  func validateError(t *testing.T, err error) {
   208  	if err != nil {
   209  		t.Errorf(err.Error())
   210  	}
   211  }