Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Created a stream like facility to read and parse JSON text.

  • Loading branch information...
commit 3e3a0a5614103e99db84335a1dc756e055a09f8e 1 parent 76894fa
Dale Ragan dragan authored
972 src/SineSignal.Ottoman.Specs/Serialization/JsonReaderSpecs/ReadingArraySpecs.cs
... ... @@ -0,0 +1,972 @@
  1 +using System;
  2 +
  3 +using NSubstitute;
  4 +using NUnit.Framework;
  5 +using SineSignal.Ottoman.Specs.Framework;
  6 +
  7 +using SineSignal.Ottoman.Serialization;
  8 +
  9 +namespace SineSignal.Ottoman.Specs.Serialization.JsonReaderSpecs
  10 +{
  11 + public class ReadingArraySpecs
  12 + {
  13 + public class When_reading_a_json_string_that_contains_an_array_with_an_empty_string : ConcernFor<JsonReader>
  14 + {
  15 + private string input;
  16 +
  17 + protected override void Given()
  18 + {
  19 + input = "[ \"\" ]";
  20 + }
  21 +
  22 + public override JsonReader CreateSystemUnderTest()
  23 + {
  24 + return new JsonReader(input);
  25 + }
  26 +
  27 + public class And_when_calling_read_twice : When_reading_a_json_string_that_contains_an_array_with_an_empty_string
  28 + {
  29 + protected override void When()
  30 + {
  31 + Sut.Read();
  32 + Sut.Read();
  33 + Sut.Close();
  34 + }
  35 +
  36 + [Test]
  37 + public void Should_set_current_token_value_to_an_empty_string()
  38 + {
  39 + Assert.That(Sut.CurrentTokenValue, Is.EqualTo(String.Empty));
  40 + }
  41 + }
  42 + }
  43 +
  44 + public class When_reading_a_json_string_that_contains_an_array_with_two_booleans : ConcernFor<JsonReader>
  45 + {
  46 + private string input;
  47 +
  48 + protected override void Given()
  49 + {
  50 + input = "[ true, false ]";
  51 + }
  52 +
  53 + public override JsonReader CreateSystemUnderTest()
  54 + {
  55 + return new JsonReader(input);
  56 + }
  57 +
  58 + public class And_when_calling_read_once : When_reading_a_json_string_that_contains_an_array_with_two_booleans
  59 + {
  60 + protected override void When()
  61 + {
  62 + Sut.Read();
  63 + }
  64 +
  65 + [Test]
  66 + public void Should_set_current_token_to_array_start()
  67 + {
  68 + Assert.That(Sut.CurrentToken, Is.EqualTo(JsonToken.ArrayStart));
  69 + }
  70 + }
  71 +
  72 + public class And_when_calling_read_twice : When_reading_a_json_string_that_contains_an_array_with_two_booleans
  73 + {
  74 + protected override void When()
  75 + {
  76 + Sut.Read();
  77 + Sut.Read();
  78 + }
  79 +
  80 + [Test]
  81 + public void Should_set_current_token_to_json_boolean()
  82 + {
  83 + Assert.That(Sut.CurrentToken, Is.EqualTo(JsonToken.Boolean));
  84 + }
  85 +
  86 + [Test]
  87 + public void Should_set_current_token_value_to_true()
  88 + {
  89 + Assert.That(Sut.CurrentTokenValue, Is.True);
  90 + }
  91 + }
  92 +
  93 + public class And_when_calling_read_three_times : When_reading_a_json_string_that_contains_an_array_with_two_booleans
  94 + {
  95 + protected override void When()
  96 + {
  97 + for (int index = 1; index <= 3; index++)
  98 + {
  99 + Sut.Read();
  100 + }
  101 + }
  102 +
  103 + [Test]
  104 + public void Should_set_current_token_to_json_boolean()
  105 + {
  106 + Assert.That(Sut.CurrentToken, Is.EqualTo(JsonToken.Boolean));
  107 + }
  108 +
  109 + [Test]
  110 + public void Should_set_current_token_value_to_false()
  111 + {
  112 + Assert.That(Sut.CurrentTokenValue, Is.False);
  113 + }
  114 + }
  115 +
  116 + public class And_when_calling_read_four_times : When_reading_a_json_string_that_contains_an_array_with_two_booleans
  117 + {
  118 + protected override void When()
  119 + {
  120 + for (int index = 1; index <= 4; index++)
  121 + {
  122 + Sut.Read();
  123 + }
  124 + }
  125 +
  126 + [Test]
  127 + public void Should_set_current_token_to_array_end()
  128 + {
  129 + Assert.That(Sut.CurrentToken, Is.EqualTo(JsonToken.ArrayEnd));
  130 + }
  131 + }
  132 +
  133 + public class And_when_calling_read_five_times : When_reading_a_json_string_that_contains_an_array_with_two_booleans
  134 + {
  135 + protected override void When()
  136 + {
  137 + for (int index = 1; index <= 5; index++)
  138 + {
  139 + Sut.Read();
  140 + }
  141 + }
  142 +
  143 + [Test]
  144 + public void Should_set_end_of_json_to_true()
  145 + {
  146 + Assert.That(Sut.EndOfJson, Is.True);
  147 + }
  148 + }
  149 + }
  150 +
  151 + public class When_reading_a_json_string_that_contains_an_array_containing_four_strings : ConcernFor<JsonReader>
  152 + {
  153 + private string input;
  154 + private string stringValue3;
  155 + private string stringValue4;
  156 +
  157 + protected override void Given()
  158 + {
  159 + input = "[ \"One\", \"Two\", \"abc 123 \\n\\f\\b\\t\\r \\\" \\\\ \\u263a \\u25CF\", \"\\\"Hello\\\" \\'world\\'\" ]";
  160 + stringValue3 = "abc 123 \n\f\b\t\r \" \\ \u263a \u25cf";
  161 + stringValue4 = "\"Hello\" 'world'";
  162 + }
  163 +
  164 + public override JsonReader CreateSystemUnderTest()
  165 + {
  166 + return new JsonReader(input);
  167 + }
  168 +
  169 + public class And_when_calling_read_once : When_reading_a_json_string_that_contains_an_array_containing_four_strings
  170 + {
  171 + protected override void When()
  172 + {
  173 + Sut.Read();
  174 + }
  175 +
  176 + [Test]
  177 + public void Should_set_current_token_to_array_start()
  178 + {
  179 + Assert.That(Sut.CurrentToken, Is.EqualTo(JsonToken.ArrayStart));
  180 + }
  181 + }
  182 +
  183 + public class And_when_calling_read_twice : When_reading_a_json_string_that_contains_an_array_containing_four_strings
  184 + {
  185 + protected override void When()
  186 + {
  187 + Sut.Read();
  188 + Sut.Read();
  189 + }
  190 +
  191 + [Test]
  192 + public void Should_set_current_token_to_json_string()
  193 + {
  194 + Assert.That(Sut.CurrentToken, Is.EqualTo(JsonToken.String));
  195 + }
  196 +
  197 + [Test]
  198 + public void Should_set_current_token_value_to_One()
  199 + {
  200 + Assert.That(Sut.CurrentTokenValue, Is.EqualTo("One"));
  201 + }
  202 + }
  203 +
  204 + public class And_when_calling_read_three_times : When_reading_a_json_string_that_contains_an_array_containing_four_strings
  205 + {
  206 + protected override void When()
  207 + {
  208 + for (int index = 1; index <= 3; index++)
  209 + {
  210 + Sut.Read();
  211 + }
  212 + }
  213 +
  214 + [Test]
  215 + public void Should_set_current_token_to_json_string()
  216 + {
  217 + Assert.That(Sut.CurrentToken, Is.EqualTo(JsonToken.String));
  218 + }
  219 +
  220 + [Test]
  221 + public void Should_set_current_token_value_to_Two()
  222 + {
  223 + Assert.That(Sut.CurrentTokenValue, Is.EqualTo("Two"));
  224 + }
  225 + }
  226 +
  227 + public class And_when_calling_read_four_times : When_reading_a_json_string_that_contains_an_array_containing_four_strings
  228 + {
  229 + protected override void When()
  230 + {
  231 + for (int index = 1; index <= 4; index++)
  232 + {
  233 + Sut.Read();
  234 + }
  235 + }
  236 +
  237 + [Test]
  238 + public void Should_set_current_token_to_json_string()
  239 + {
  240 + Assert.That(Sut.CurrentToken, Is.EqualTo(JsonToken.String));
  241 + }
  242 +
  243 + [Test]
  244 + public void Should_set_current_token_value_to_stringValue3()
  245 + {
  246 + Assert.That(Sut.CurrentTokenValue, Is.EqualTo(stringValue3));
  247 + }
  248 + }
  249 +
  250 + public class And_when_calling_read_five_times : When_reading_a_json_string_that_contains_an_array_containing_four_strings
  251 + {
  252 + protected override void When()
  253 + {
  254 + for (int index = 1; index <= 5; index++)
  255 + {
  256 + Sut.Read();
  257 + }
  258 + }
  259 +
  260 + [Test]
  261 + public void Should_set_current_token_to_json_string()
  262 + {
  263 + Assert.That(Sut.CurrentToken, Is.EqualTo(JsonToken.String));
  264 + }
  265 +
  266 + [Test]
  267 + public void Should_set_current_token_value_to_stringValue4()
  268 + {
  269 + Assert.That(Sut.CurrentTokenValue, Is.EqualTo(stringValue4));
  270 + }
  271 + }
  272 +
  273 + public class And_when_calling_read_six_times : When_reading_a_json_string_that_contains_an_array_containing_four_strings
  274 + {
  275 + protected override void When()
  276 + {
  277 + for (int index = 1; index <= 6; index++)
  278 + {
  279 + Sut.Read();
  280 + }
  281 + }
  282 +
  283 + [Test]
  284 + public void Should_set_current_token_to_array_end()
  285 + {
  286 + Assert.That(Sut.CurrentToken, Is.EqualTo(JsonToken.ArrayEnd));
  287 + }
  288 + }
  289 +
  290 + public class And_when_calling_read_seven_times : When_reading_a_json_string_that_contains_an_array_containing_four_strings
  291 + {
  292 + protected override void When()
  293 + {
  294 + for (int index = 1; index <= 7; index++)
  295 + {
  296 + Sut.Read();
  297 + }
  298 + }
  299 +
  300 + [Test]
  301 + public void Should_set_end_of_json_to_true()
  302 + {
  303 + Assert.That(Sut.EndOfJson, Is.True);
  304 + }
  305 + }
  306 + }
  307 +
  308 + public class When_reading_a_json_string_that_contains_an_array_of_ints : ConcernFor<JsonReader>
  309 + {
  310 + private string input;
  311 +
  312 + protected override void Given()
  313 + {
  314 + input = @"[ -10, -5, -0, 0, 5, 10 ]";
  315 + }
  316 +
  317 + public override JsonReader CreateSystemUnderTest()
  318 + {
  319 + return new JsonReader(input);
  320 + }
  321 +
  322 + public class And_when_calling_read_twice : When_reading_a_json_string_that_contains_an_array_of_ints
  323 + {
  324 + protected override void When()
  325 + {
  326 + Sut.Read();
  327 + Sut.Read();
  328 + }
  329 +
  330 + [Test]
  331 + public void Should_set_current_token_value_to_negative_10()
  332 + {
  333 + Assert.That((int)Sut.CurrentTokenValue, Is.EqualTo(-10));
  334 + }
  335 + }
  336 +
  337 + public class And_when_calling_read_three_times : When_reading_a_json_string_that_contains_an_array_of_ints
  338 + {
  339 + protected override void When()
  340 + {
  341 + for (int index = 1; index <= 3; index++)
  342 + {
  343 + Sut.Read();
  344 + }
  345 + }
  346 +
  347 + [Test]
  348 + public void Should_set_current_token_value_to_negative_5()
  349 + {
  350 + Assert.That((int)Sut.CurrentTokenValue, Is.EqualTo(-5));
  351 + }
  352 + }
  353 +
  354 + public class And_when_calling_read_four_times : When_reading_a_json_string_that_contains_an_array_of_ints
  355 + {
  356 + protected override void When()
  357 + {
  358 + for (int index = 1; index <= 4; index++)
  359 + {
  360 + Sut.Read();
  361 + }
  362 + }
  363 +
  364 + [Test]
  365 + public void Should_set_current_token_value_to_0()
  366 + {
  367 + Assert.That((int)Sut.CurrentTokenValue, Is.EqualTo(0));
  368 + }
  369 + }
  370 +
  371 + public class And_when_calling_read_five_times : When_reading_a_json_string_that_contains_an_array_of_ints
  372 + {
  373 + protected override void When()
  374 + {
  375 + for (int index = 1; index <= 5; index++)
  376 + {
  377 + Sut.Read();
  378 + }
  379 + }
  380 +
  381 + [Test]
  382 + public void Should_set_current_token_value_to_0()
  383 + {
  384 + Assert.That((int)Sut.CurrentTokenValue, Is.EqualTo(0));
  385 + }
  386 + }
  387 +
  388 + public class And_when_calling_read_six_times : When_reading_a_json_string_that_contains_an_array_of_ints
  389 + {
  390 + protected override void When()
  391 + {
  392 + for (int index = 1; index <= 6; index++)
  393 + {
  394 + Sut.Read();
  395 + }
  396 + }
  397 +
  398 + [Test]
  399 + public void Should_set_current_token_value_to_5()
  400 + {
  401 + Assert.That((int)Sut.CurrentTokenValue, Is.EqualTo(5));
  402 + }
  403 + }
  404 +
  405 + public class And_when_calling_read_seven_times : When_reading_a_json_string_that_contains_an_array_of_ints
  406 + {
  407 + protected override void When()
  408 + {
  409 + for (int index = 1; index <= 7; index++)
  410 + {
  411 + Sut.Read();
  412 + }
  413 + }
  414 +
  415 + [Test]
  416 + public void Should_set_current_token_value_to_10()
  417 + {
  418 + Assert.That((int)Sut.CurrentTokenValue, Is.EqualTo(10));
  419 + }
  420 + }
  421 + }
  422 +
  423 + public class When_reading_a_json_string_that_contains_an_array_of_doubles : ConcernFor<JsonReader>
  424 + {
  425 + private string input;
  426 +
  427 + protected override void Given()
  428 + {
  429 + input = @"[ -125.000009, 10.0, -10.0, 0.0, -0.0, 3.1415926536, 5e-4, 233e+5, 0.6e2, 2E-5 ]";
  430 + }
  431 +
  432 + public override JsonReader CreateSystemUnderTest()
  433 + {
  434 + return new JsonReader(input);
  435 + }
  436 +
  437 + public class And_when_calling_read_twice : When_reading_a_json_string_that_contains_an_array_of_doubles
  438 + {
  439 + protected override void When()
  440 + {
  441 + Sut.Read();
  442 + Sut.Read();
  443 + }
  444 +
  445 + [Test]
  446 + public void Should_set_current_token_value_to_negative_125_dot_000009()
  447 + {
  448 + Assert.That((double)Sut.CurrentTokenValue, Is.EqualTo(-125.000009));
  449 + }
  450 + }
  451 +
  452 + public class And_when_calling_read_three_times : When_reading_a_json_string_that_contains_an_array_of_doubles
  453 + {
  454 + protected override void When()
  455 + {
  456 + for (int index = 1; index <= 3; index++)
  457 + {
  458 + Sut.Read();
  459 + }
  460 + }
  461 +
  462 + [Test]
  463 + public void Should_set_current_token_value_to_10_dot_0()
  464 + {
  465 + Assert.That((double)Sut.CurrentTokenValue, Is.EqualTo(10.0));
  466 + }
  467 + }
  468 +
  469 + public class And_when_calling_read_four_times : When_reading_a_json_string_that_contains_an_array_of_doubles
  470 + {
  471 + protected override void When()
  472 + {
  473 + for (int index = 1; index <= 4; index++)
  474 + {
  475 + Sut.Read();
  476 + }
  477 + }
  478 +
  479 + [Test]
  480 + public void Should_set_current_token_value_to_negative_10_dot_0()
  481 + {
  482 + Assert.That((double)Sut.CurrentTokenValue, Is.EqualTo(-10.0));
  483 + }
  484 + }
  485 +
  486 + public class And_when_calling_read_five_times : When_reading_a_json_string_that_contains_an_array_of_doubles
  487 + {
  488 + protected override void When()
  489 + {
  490 + for (int index = 1; index <= 5; index++)
  491 + {
  492 + Sut.Read();
  493 + }
  494 + }
  495 +
  496 + [Test]
  497 + public void Should_set_current_token_value_to_0_dot_0()
  498 + {
  499 + Assert.That((double)Sut.CurrentTokenValue, Is.EqualTo(0.0));
  500 + }
  501 + }
  502 +
  503 + public class And_when_calling_read_six_times : When_reading_a_json_string_that_contains_an_array_of_doubles
  504 + {
  505 + protected override void When()
  506 + {
  507 + for (int index = 1; index <= 6; index++)
  508 + {
  509 + Sut.Read();
  510 + }
  511 + }
  512 +
  513 + [Test]
  514 + public void Should_set_current_token_value_to_0_dot_0()
  515 + {
  516 + Assert.That((double)Sut.CurrentTokenValue, Is.EqualTo(0.0));
  517 + }
  518 + }
  519 +
  520 + public class And_when_calling_read_seven_times : When_reading_a_json_string_that_contains_an_array_of_doubles
  521 + {
  522 + protected override void When()
  523 + {
  524 + for (int index = 1; index <= 7; index++)
  525 + {
  526 + Sut.Read();
  527 + }
  528 + }
  529 +
  530 + [Test]
  531 + public void Should_set_current_token_value_to_3_dot_1415926536()
  532 + {
  533 + Assert.That((double)Sut.CurrentTokenValue, Is.EqualTo(3.1415926536));
  534 + }
  535 + }
  536 +
  537 + public class And_when_calling_read_eight_times : When_reading_a_json_string_that_contains_an_array_of_doubles
  538 + {
  539 + protected override void When()
  540 + {
  541 + for (int index = 1; index <= 8; index++)
  542 + {
  543 + Sut.Read();
  544 + }
  545 + }
  546 +
  547 + [Test]
  548 + public void Should_set_current_token_value_to_0_dot_0005()
  549 + {
  550 + Assert.That((double)Sut.CurrentTokenValue, Is.EqualTo(0.0005));
  551 + }
  552 + }
  553 +
  554 + public class And_when_calling_read_nine_times : When_reading_a_json_string_that_contains_an_array_of_doubles
  555 + {
  556 + protected override void When()
  557 + {
  558 + for (int index = 1; index <= 9; index++)
  559 + {
  560 + Sut.Read();
  561 + }
  562 + }
  563 +
  564 + [Test]
  565 + public void Should_set_current_token_value_to_23300000_dot_0()
  566 + {
  567 + Assert.That((double)Sut.CurrentTokenValue, Is.EqualTo(23300000.0));
  568 + }
  569 + }
  570 +
  571 + public class And_when_calling_read_ten_times : When_reading_a_json_string_that_contains_an_array_of_doubles
  572 + {
  573 + protected override void When()
  574 + {
  575 + for (int index = 1; index <= 10; index++)
  576 + {
  577 + Sut.Read();
  578 + }
  579 + }
  580 +
  581 + [Test]
  582 + public void Should_set_current_token_value_to_60_dot_0()
  583 + {
  584 + Assert.That((double)Sut.CurrentTokenValue, Is.EqualTo(60.0));
  585 + }
  586 + }
  587 +
  588 + public class And_when_calling_read_eleven_times : When_reading_a_json_string_that_contains_an_array_of_doubles
  589 + {
  590 + protected override void When()
  591 + {
  592 + for (int index = 1; index <= 11; index++)
  593 + {
  594 + Sut.Read();
  595 + }
  596 + }
  597 +
  598 + [Test]
  599 + public void Should_set_current_token_value_to_0_dot_00002()
  600 + {
  601 + Assert.That((double)Sut.CurrentTokenValue, Is.EqualTo(0.00002));
  602 + }
  603 + }
  604 + }
  605 +
  606 + public class When_reading_a_json_string_that_contains_an_error_in_the_escape_sequence : ConcernFor<JsonReader>
  607 + {
  608 + private string input;
  609 + private JsonException expectedException;
  610 +
  611 + protected override void Given()
  612 + {
  613 + input = "[ \"Hello World \\ufffg \" ]";
  614 + }
  615 +
  616 + public override JsonReader CreateSystemUnderTest()
  617 + {
  618 + return new JsonReader(input);
  619 + }
  620 +
  621 + protected override void When()
  622 + {
  623 + try
  624 + {
  625 + while (Sut.Read());
  626 + }
  627 + catch (JsonException e)
  628 + {
  629 + expectedException = e;
  630 + }
  631 + }
  632 +
  633 + [Test]
  634 + public void Should_throw_a_json_exception()
  635 + {
  636 + Assert.That(expectedException, Is.TypeOf(typeof(JsonException)));
  637 + }
  638 +
  639 + [Test]
  640 + public void Should_set_message_to_Invalid_character_g_in_input_string()
  641 + {
  642 + Assert.That(expectedException.Message, Is.EqualTo("Invalid character 'g' in input string"));
  643 + }
  644 + }
  645 +
  646 + public class When_reading_a_json_string_that_contains_an_invalid_real_number : ConcernFor<JsonReader>
  647 + {
  648 + private string input;
  649 + private JsonException expectedException;
  650 +
  651 + protected override void Given()
  652 + {
  653 + input = "[ 0.e5 ]";
  654 + }
  655 +
  656 + public override JsonReader CreateSystemUnderTest()
  657 + {
  658 + return new JsonReader(input);
  659 + }
  660 +
  661 + protected override void When()
  662 + {
  663 + try
  664 + {
  665 + while (Sut.Read());
  666 + }
  667 + catch (JsonException e)
  668 + {
  669 + expectedException = e;
  670 + }
  671 + }
  672 +
  673 + [Test]
  674 + public void Should_throw_a_json_exception()
  675 + {
  676 + Assert.That(expectedException, Is.TypeOf(typeof(JsonException)));
  677 + }
  678 +
  679 + [Test]
  680 + public void Should_set_message_to_Invalid_character_e_in_input_string()
  681 + {
  682 + Assert.That(expectedException.Message, Is.EqualTo("Invalid character 'e' in input string"));
  683 + }
  684 + }
  685 +
  686 + public class When_reading_a_json_string_that_contains_an_invalid_boolean_value : ConcernFor<JsonReader>
  687 + {
  688 + private string input;
  689 + private JsonException expectedException;
  690 +
  691 + protected override void Given()
  692 + {
  693 + input = "[ TRUE ]";
  694 + }
  695 +
  696 + public override JsonReader CreateSystemUnderTest()
  697 + {
  698 + return new JsonReader(input);
  699 + }
  700 +
  701 + protected override void When()
  702 + {
  703 + try
  704 + {
  705 + while (Sut.Read());
  706 + }
  707 + catch (JsonException e)
  708 + {
  709 + expectedException = e;
  710 + }
  711 + }
  712 +
  713 + [Test]
  714 + public void Should_throw_a_json_exception()
  715 + {
  716 + Assert.That(expectedException, Is.TypeOf(typeof(JsonException)));
  717 + }
  718 +
  719 + [Test]
  720 + public void Should_set_message_to_Invalid_character_T_in_input_string()
  721 + {
  722 + Assert.That(expectedException.Message, Is.EqualTo("Invalid character 'T' in input string"));
  723 + }
  724 + }
  725 +
  726 + public class When_reading_a_json_string_that_contains_an_array_with_the_wrong_closing_token : ConcernFor<JsonReader>
  727 + {
  728 + private string input;
  729 + private JsonException expectedException;
  730 +
  731 + protected override void Given()
  732 + {
  733 + input = "[ 1, 2, 3 }";
  734 + }
  735 +
  736 + public override JsonReader CreateSystemUnderTest()
  737 + {
  738 + return new JsonReader(input);
  739 + }
  740 +
  741 + protected override void When()
  742 + {
  743 + try
  744 + {
  745 + while (Sut.Read());
  746 + }
  747 + catch (JsonException e)
  748 + {
  749 + expectedException = e;
  750 + }
  751 + }
  752 +
  753 + [Test]
  754 + public void Should_throw_a_json_exception()
  755 + {
  756 + Assert.That(expectedException, Is.TypeOf(typeof(JsonException)));
  757 + }
  758 +
  759 + [Test]
  760 + public void Should_set_message_to_Invalid_token_125_in_input_string()
  761 + {
  762 + Assert.That(expectedException.Message, Is.EqualTo("Invalid token '125' in input string"));
  763 + }
  764 + }
  765 +
  766 + public class When_reading_a_json_string_that_contains_an_array_with_no_closing_token : ConcernFor<JsonReader>
  767 + {
  768 + private string input;
  769 + private JsonException expectedException;
  770 +
  771 + protected override void Given()
  772 + {
  773 + input = "[ \"name1\" ";
  774 + }
  775 +
  776 + public override JsonReader CreateSystemUnderTest()
  777 + {
  778 + return new JsonReader(input);
  779 + }
  780 +
  781 + protected override void When()
  782 + {
  783 + try
  784 + {
  785 + while (Sut.Read());
  786 + }
  787 + catch (JsonException e)
  788 + {
  789 + expectedException = e;
  790 + }
  791 + }
  792 +
  793 + [Test]
  794 + public void Should_throw_a_json_exception()
  795 + {
  796 + Assert.That(expectedException, Is.TypeOf(typeof(JsonException)));
  797 + }
  798 +
  799 + [Test]
  800 + public void Should_set_message_to_Input_does_not_evaluate_to_proper_JSON_text()
  801 + {
  802 + Assert.That(expectedException.Message, Is.EqualTo("Input doesn't evaluate to proper JSON text"));
  803 + }
  804 + }
  805 +
  806 + public class When_reading_a_json_string_that_contains_no_array_or_object_tokens : ConcernFor<JsonReader>
  807 + {
  808 + private string input;
  809 + private JsonException expectedException;
  810 +
  811 + protected override void Given()
  812 + {
  813 + input = " true ";
  814 + }
  815 +
  816 + public override JsonReader CreateSystemUnderTest()
  817 + {
  818 + return new JsonReader(input);
  819 + }
  820 +
  821 + protected override void When()
  822 + {
  823 + try
  824 + {
  825 + while (Sut.Read());
  826 + }
  827 + catch (JsonException e)
  828 + {
  829 + expectedException = e;
  830 + }
  831 + }
  832 +
  833 + [Test]
  834 + public void Should_throw_a_json_exception()
  835 + {
  836 + Assert.That(expectedException, Is.TypeOf(typeof(JsonException)));
  837 + }
  838 +
  839 + [Test]
  840 + public void Should_set_message_to_Invalid_token_True_in_input_string()
  841 + {
  842 + Assert.That(expectedException.Message, Is.EqualTo("Invalid token 'True' in input string"));
  843 + }
  844 + }
  845 +
  846 + public class When_reading_a_json_string_with_nested_arrays : ConcernFor<JsonReader>
  847 + {
  848 + private string input;
  849 + private int arrayCount;
  850 +
  851 + protected override void Given()
  852 + {
  853 + input = "[ [ [ [ [ 1, 2, 3 ] ] ] ] ]";
  854 + arrayCount = 0;
  855 + }
  856 +
  857 + public override JsonReader CreateSystemUnderTest()
  858 + {
  859 + return new JsonReader(input);
  860 + }
  861 +
  862 + protected override void When()
  863 + {
  864 + while (Sut.Read())
  865 + {
  866 + if (Sut.CurrentToken == JsonToken.ArrayStart)
  867 + {
  868 + arrayCount++;
  869 + }
  870 + }
  871 + }
  872 +
  873 + [Test]
  874 + public void Should_count_five_arrays()
  875 + {
  876 + Assert.That(arrayCount, Is.EqualTo(5));
  877 + }
  878 + }
  879 +
  880 + public class When_reading_a_json_string_that_contains_an_array_with_comments : ConcernFor<JsonReader>
  881 + {
  882 + private string input;
  883 + private JsonException expectedException;
  884 +
  885 + protected override void Given()
  886 + {
  887 + input = @"
  888 + [
  889 + // This is a comment
  890 + 1,
  891 + 2,
  892 + 3
  893 + ]";
  894 + }
  895 +
  896 + public override JsonReader CreateSystemUnderTest()
  897 + {
  898 + return new JsonReader(input);
  899 + }
  900 +
  901 + public class And_when_comments_are_not_allowed : When_reading_a_json_string_that_contains_an_array_with_comments
  902 + {
  903 + protected override void When()
  904 + {
  905 + try
  906 + {
  907 + while (Sut.Read());
  908 + }
  909 + catch (JsonException e)
  910 + {
  911 + expectedException = e;
  912 + }
  913 + }
  914 +
  915 + [Test]
  916 + public void Should_throw_a_json_exception()
  917 + {
  918 + Assert.That(expectedException, Is.TypeOf(typeof(JsonException)));
  919 + }
  920 +
  921 + [Test]
  922 + public void Should_set_message_to_Invalid_character_forward_slash_in_input_string()
  923 + {
  924 + Assert.That(expectedException.Message, Is.EqualTo("Invalid character '/' in input string"));
  925 + }
  926 + }
  927 + }
  928 +
  929 + public class When_reading_a_json_string_that_contains_an_array_with_a_string_in_single_quotes : ConcernFor<JsonReader>
  930 + {
  931 + private string input;
  932 + private JsonException expectedException;
  933 +
  934 + protected override void Given()
  935 + {
  936 + input = "[ 'Single quotes' ]";
  937 + }
  938 +
  939 + public override JsonReader CreateSystemUnderTest()
  940 + {
  941 + return new JsonReader(input);
  942 + }
  943 +
  944 + public class And_when_single_quotes_are_not_allowed : When_reading_a_json_string_that_contains_an_array_with_a_string_in_single_quotes
  945 + {
  946 + protected override void When()
  947 + {
  948 + try
  949 + {
  950 + while (Sut.Read());
  951 + }
  952 + catch (JsonException e)
  953 + {
  954 + expectedException = e;
  955 + }
  956 + }
  957 +
  958 + [Test]
  959 + public void Should_throw_a_json_exception()
  960 + {
  961 + Assert.That(expectedException, Is.TypeOf(typeof(JsonException)));
  962 + }
  963 +
  964 + [Test]
  965 + public void Should_set_message_to_Invalid_character_single_quote_in_input_string()
  966 + {
  967 + Assert.That(expectedException.Message, Is.EqualTo("Invalid character ''' in input string"));
  968 + }
  969 + }
  970 + }
  971 + }
  972 +}
407 src/SineSignal.Ottoman.Specs/Serialization/JsonReaderSpecs/ReadingObjectsSpecs.cs
... ... @@ -0,0 +1,407 @@
  1 +using System;
  2 +
  3 +using NSubstitute;
  4 +using NUnit.Framework;
  5 +using SineSignal.Ottoman.Specs.Framework;
  6 +
  7 +using SineSignal.Ottoman.Serialization;
  8 +
  9 +namespace SineSignal.Ottoman.Specs.Serialization.JsonReaderSpecs
  10 +{
  11 + public class ReadingObjectsSpecs
  12 + {
  13 + public class When_reading_a_json_string_that_contains_an_object_with_a_single_member : ConcernFor<JsonReader>
  14 + {
  15 + private string input;
  16 +
  17 + protected override void Given()
  18 + {
  19 + input = "{\"name1\":true}";
  20 + }
  21 +
  22 + public override JsonReader CreateSystemUnderTest()
  23 + {
  24 + return new JsonReader(input);
  25 + }
  26 +
  27 + public class And_when_calling_read_once : When_reading_a_json_string_that_contains_an_object_with_a_single_member
  28 + {
  29 + protected override void When()
  30 + {
  31 + Sut.Read();
  32 + }
  33 +
  34 + [Test]
  35 + public void Should_set_current_token_to_object_start()
  36 + {
  37 + Assert.That(Sut.CurrentToken, Is.EqualTo(JsonToken.ObjectStart));
  38 + }
  39 + }
  40 +
  41 + public class And_when_calling_read_twice : When_reading_a_json_string_that_contains_an_object_with_a_single_member
  42 + {