aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/yaml/snakeyaml/issues/issue29/FlexibleScalarStyleTest.java
blob: f5f1d2360b4a7b3f3551d63fb2d327f16bbfb297 (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
/**
 * Copyright (c) 2008, SnakeYAML
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package org.yaml.snakeyaml.issues.issue29;

import java.util.LinkedHashMap;
import java.util.Map;
import junit.framework.TestCase;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.DumperOptions.ScalarStyle;
import org.yaml.snakeyaml.Util;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.ScalarNode;
import org.yaml.snakeyaml.representer.Representer;

/**
 * to test http://code.google.com/p/snakeyaml/issues/detail?id=29
 */
public class FlexibleScalarStyleTest extends TestCase {

  public void testLong() {
    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(ScalarStyle.FOLDED);
    Yaml yaml = new Yaml(options);
    String result =
        yaml.dump("qqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqq "
            + "qqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqq "
            + "qqqqqqqqqqqqqqqqqqqqqqqqq 111111111111111111111111\n "
            + "qqqqqqqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqqqq\n");
    // System.out.println(result);
    assertTrue(result.startsWith(">\n"));
    assertEquals(
        ">\n  qqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqq\n  qqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqq 111111111111111111111111\n   qqqqqqqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqqqq\n",
        result);
  }

  public void testDefaultScalarStyle() {
    DumperOptions options = new DumperOptions();
    options.setWidth(30);
    Yaml yaml = new Yaml(options);
    String output = yaml.dump(getData());
    // System.out.println(output);
    String etalon = Util.getLocalResource("representer/scalar-style1.yaml");
    assertEquals(etalon, output);
  }

  public void testCustomScalarStyle() {
    DumperOptions options = new DumperOptions();
    options.setWidth(30);
    Yaml yaml = new Yaml(new MyRepresenter(), options);
    String output = yaml.dump(getData());
    // System.out.println(output);
    String etalon = Util.getLocalResource("representer/scalar-style2.yaml");
    assertEquals(etalon, output);
  }

  public void testCustomScalarStyleNoSplitLines() {
    DumperOptions options = new DumperOptions();
    options.setWidth(30);
    options.setSplitLines(false);
    Yaml yaml = new Yaml(new MyRepresenter(), options);
    String output = yaml.dump(getData());
    // System.out.println(output);
    String etalon = Util.getLocalResource("representer/scalar-style3.yaml");
    assertEquals(etalon, output);
  }

  private Map<String, String> getData() {
    Map<String, String> map = new LinkedHashMap<String, String>();
    map.put("name", "Steve Jobs");
    map.put("address", "Name\nStreet Number\nCountry");
    map.put("description",
        "1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000");
    return map;
  }

  private class MyRepresenter extends Representer {

    public MyRepresenter() {
      super();
      this.representers.put(String.class, new FlexibleRepresent());
    }

    private class FlexibleRepresent extends RepresentString {

      public Node representData(Object data) {
        ScalarNode node = (ScalarNode) super.representData(data);
        if (node.isPlain()) {
          // if Plain scalar style
          if (node.getValue().length() < 25) {
            return node;
          } else {
            // Folded scalar style
            return new ScalarNode(node.getTag(), node.getValue(), node.getStartMark(),
                node.getEndMark(), ScalarStyle.FOLDED);
          }
        } else {
          return node;
        }
      }
    }
  }
}