11<?php namespace text \json \unittest ;
22
3- use lang \FormatException ;
3+ use io \streams \{MemoryInputStream , MemoryOutputStream };
4+ use io \{Files , TempFile };
5+ use lang \{FormatException , IllegalArgumentException };
46use test \{Assert , Expect , Test };
57use text \json \{Format , Json , StringInput , StringOutput };
8+ use util \Bytes ;
69
710class JsonTest {
811
@@ -16,6 +19,26 @@ public function read_string() {
1619 Assert::equals ('Test ' , Json::read ('"Test" ' ));
1720 }
1821
22+ #[Test]
23+ public function read_casts_input_to_string () {
24+ Assert::equals ('Test ' , Json::read (new Bytes ('"Test" ' )));
25+ }
26+
27+ #[Test]
28+ public function read_file () {
29+ $ file = new TempFile ();
30+ try {
31+ Assert::equals ('Test ' , Json::read ($ file ->containing ('"Test" ' )));
32+ } finally {
33+ $ file ->unlink ();
34+ }
35+ }
36+
37+ #[Test]
38+ public function read_stream () {
39+ Assert::equals ('Test ' , Json::read (new MemoryInputStream ('"Test" ' )));
40+ }
41+
1942 #[Test, Expect(FormatException::class)]
2043 public function read_malformed_string () {
2144 Json::read ('this.is.not.json ' );
@@ -31,6 +54,26 @@ public function write_output() {
3154 Assert::equals ('"Test" ' , Json::write ('Test ' , new StringOutput ())->bytes ());
3255 }
3356
57+ #[Test]
58+ public function write_file () {
59+ $ file = new TempFile ();
60+ try {
61+ Assert::equals ('"Test" ' , Files::read (Json::write ('Test ' , $ file )->file ()));
62+ } finally {
63+ $ file ->unlink ();
64+ }
65+ }
66+
67+ #[Test]
68+ public function write_stream () {
69+ Assert::equals ('"Test" ' , Json::write ('Test ' , new MemoryOutputStream ())->stream ()->bytes ());
70+ }
71+
72+ #[Test, Expect(IllegalArgumentException::class)]
73+ public function write_incorrect_type () {
74+ Json::write ('Test ' , $ this );
75+ }
76+
3477 #[Test]
3578 public function of_string () {
3679 Assert::equals ('"Test" ' , Json::of ('Test ' ));
0 commit comments