1 
2 //          Copyright Ferdinand Majerech 2011-2014
3 // Distributed under the Boost Software License, Version 1.0.
4 //    (See accompanying file LICENSE_1_0.txt or copy at
5 //          http://www.boost.org/LICENSE_1_0.txt)
6 
7 module dyaml.testerrors;
8 
9 
10 version(unittest)
11 {
12 
13 import std.file;
14 
15 import dyaml.testcommon;
16 
17 
18 /// Loader error unittest from file stream.
19 ///
20 /// Params:  verbose       = Print verbose output?
21 ///          errorFilename = File name to read from.
22 void testLoaderError(bool verbose, string errorFilename)
23 {
24     auto buffer = std.file.read(errorFilename);
25 
26     Node[] nodes;
27     try { nodes = Loader(buffer).loadAll(); }
28     catch(YAMLException e)
29     {
30         if(verbose) { writeln(typeid(e).toString(), "\n", e); }
31         return;
32     }
33     assert(false, "Expected an exception");
34 }
35 
36 /// Loader error unittest from string.
37 ///
38 /// Params:  verbose       = Print verbose output?
39 ///          errorFilename = File name to read from.
40 void testLoaderErrorString(bool verbose, string errorFilename)
41 {
42     // Load file to a buffer, then pass that to the YAML loader.
43     auto buffer = std.file.read(errorFilename);
44 
45     try
46     {
47         auto nodes = Loader(buffer).loadAll();
48     }
49     catch(YAMLException e)
50     {
51         if(verbose) { writeln(typeid(e).toString(), "\n", e); }
52         return;
53     }
54     assert(false, "Expected an exception");
55 }
56 
57 /// Loader error unittest from filename.
58 ///
59 /// Params:  verbose       = Print verbose output?
60 ///          errorFilename = File name to read from.
61 void testLoaderErrorFilename(bool verbose, string errorFilename)
62 {
63     try { auto nodes = Loader(errorFilename).loadAll(); }
64     catch(YAMLException e)
65     {
66         if(verbose) { writeln(typeid(e).toString(), "\n", e); }
67         return;
68     }
69     assert(false, "testLoaderErrorSingle(" ~ verbose.to!string ~
70                   ", " ~ errorFilename ~ ") Expected an exception");
71 }
72 
73 /// Loader error unittest loading a single document from a file.
74 ///
75 /// Params:  verbose       = Print verbose output?
76 ///          errorFilename = File name to read from.
77 void testLoaderErrorSingle(bool verbose, string errorFilename)
78 {
79     try { auto nodes = Loader(errorFilename).load(); }
80     catch(YAMLException e)
81     {
82         if(verbose) { writeln(typeid(e).toString(), "\n", e); }
83         return;
84     }
85     assert(false, "Expected an exception");
86 }
87 
88 
89 unittest
90 {
91     writeln("D:YAML Errors unittest");
92     run("testLoaderError",         &testLoaderError,         ["loader-error"]);
93     run("testLoaderErrorString",   &testLoaderErrorString,   ["loader-error"]);
94     run("testLoaderErrorFilename", &testLoaderErrorFilename, ["loader-error"]);
95     run("testLoaderErrorSingle",   &testLoaderErrorSingle,   ["single-loader-error"]);
96 }
97 
98 } // version(unittest)