aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/ext_depends/D-YAML/source/dyaml/test/emitter.d
blob: 293f236a85ac1edafb07e7444bd09a483a6d9896 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//          Copyright Ferdinand Majerech 2011-2014.
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

module dyaml.test.emitter;

@safe unittest
{
    import std.array : Appender;
    import std.range : ElementType, isInputRange;

    import dyaml : CollectionStyle, LineBreak, Loader, Mark, ScalarStyle;
    import dyaml.emitter : Emitter;
    import dyaml.event : Event, EventID, mappingStartEvent, scalarEvent, sequenceStartEvent;
    import dyaml.test.common : assertEventsEqual, run;

    // Try to emit an event range.
    static void emitTestCommon(T)(ref Appender!string emitStream, T events, bool canonical = false) @safe
        if (isInputRange!T && is(ElementType!T == Event))
    {
        auto emitter = Emitter!(typeof(emitStream), char)(emitStream, canonical, 2, 80, LineBreak.unix);
        foreach (ref event; events)
        {
            emitter.emit(event);
        }
    }
    /**
    Test emitter by getting events from parsing a file, emitting them, parsing
    the emitted result and comparing events from parsing the emitted result with
    originally parsed events.

    Params:
        dataFilename = YAML file to parse.
        canonicalFilename = Canonical YAML file used as dummy to determine
            which data files to load.
    */
    static void testEmitterOnData(string dataFilename, string canonicalFilename) @safe
    {
        //Must exist due to Anchor, Tags reference counts.
        auto loader = Loader.fromFile(dataFilename);
        auto events = loader.parse();
        auto emitStream = Appender!string();
        emitTestCommon(emitStream, events);

        auto loader2 = Loader.fromString(emitStream.data);
        loader2.name = "TEST";
        auto newEvents = loader2.parse();
        assertEventsEqual(events, newEvents);
    }
    /**
    Test emitter by getting events from parsing a canonical YAML file, emitting
    them both in canonical and normal format, parsing the emitted results and
    comparing events from parsing the emitted result with originally parsed events.

    Params:  canonicalFilename = Canonical YAML file to parse.
    */
    static void testEmitterOnCanonical(string canonicalFilename) @safe
    {
        //Must exist due to Anchor, Tags reference counts.
        auto loader = Loader.fromFile(canonicalFilename);
        auto events = loader.parse();
        foreach (canonical; [false, true])
        {
            auto emitStream = Appender!string();
            emitTestCommon(emitStream, events, canonical);

            auto loader2 = Loader.fromString(emitStream.data);
            loader2.name = "TEST";
            auto newEvents = loader2.parse();
            assertEventsEqual(events, newEvents);
        }
    }
    /**
    Test emitter by getting events from parsing a file, emitting them with all
    possible scalar and collection styles, parsing the emitted results and
    comparing events from parsing the emitted result with originally parsed events.

    Params:
        dataFilename = YAML file to parse.
        canonicalFilename = Canonical YAML file used as dummy to determine
            which data files to load.
    */
    static void testEmitterStyles(string dataFilename, string canonicalFilename) @safe
    {
        foreach (filename; [dataFilename, canonicalFilename])
        {
            //must exist due to Anchor, Tags reference counts
            auto loader = Loader.fromFile(canonicalFilename);
            auto events = loader.parse();
            foreach (flowStyle; [CollectionStyle.block, CollectionStyle.flow])
            {
                foreach (style; [ScalarStyle.literal, ScalarStyle.folded,
                                ScalarStyle.doubleQuoted, ScalarStyle.singleQuoted,
                                ScalarStyle.plain])
                {
                    Event[] styledEvents;
                    foreach (event; events)
                    {
                        if (event.id == EventID.scalar)
                        {
                            event = scalarEvent(Mark(), Mark(), event.anchor, event.tag,
                                                event.implicit,
                                                event.value, style);
                        }
                        else if (event.id == EventID.sequenceStart)
                        {
                            event = sequenceStartEvent(Mark(), Mark(), event.anchor,
                                                       event.tag, event.implicit, flowStyle);
                        }
                        else if (event.id == EventID.mappingStart)
                        {
                            event = mappingStartEvent(Mark(), Mark(), event.anchor,
                                                      event.tag, event.implicit, flowStyle);
                        }
                        styledEvents ~= event;
                    }
                    auto emitStream = Appender!string();
                    emitTestCommon(emitStream, styledEvents);
                    auto loader2 = Loader.fromString(emitStream.data);
                    loader2.name = "TEST";
                    auto newEvents = loader2.parse();
                    assertEventsEqual(events, newEvents);
                }
            }
        }
    }
    run(&testEmitterOnData, ["data", "canonical"]);
    run(&testEmitterOnCanonical, ["canonical"]);
    run(&testEmitterStyles, ["data", "canonical"]);
}