aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/anarres/cpp/VaArgsPastingTest.java
blob: f1bcbcdd86ffcb4d78df6a2fb38752d3759b44ad (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
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.anarres.cpp;

import com.google.common.io.CharStreams;
import java.io.IOException;
import java.io.Reader;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.junit.Assert.*;

/**
 *
 * @author shevek
 */
public class VaArgsPastingTest {

    private static final Logger LOG = LoggerFactory.getLogger(VaArgsPastingTest.class);

    @Test
    public void testWhitespacePasting() throws IOException {
        String input
                = "#define REGULAR_ARGS(x, y) foo(x, y)\n"
                + "#define REGULAR_ELLIPSIS(x, y...) foo(x, y)\n"
                + "#define REGULAR_VAARGS(x, ...) foo(x, __VA_ARGS__)\n"
                + "#define PASTE_ARGS(x, y) foo(x, ## y)\n"
                + "#define PASTE_ELLIPSIS(x, y...) foo(x, ## y)\n"
                + "#define PASTE_VAARGS(x, ...) foo(x, ## __VA_ARGS__)\n"
                + ""
                + "REGULAR_ARGS(a, b) // REGULAR_ARGS 2\n"
                + "REGULAR_ELLIPSIS(a, b) // REGULAR_ELLIPSIS 2\n"
                + "REGULAR_ELLIPSIS(a) // REGULAR_ELLIPSIS 1\n"
                + "REGULAR_VAARGS(a, b) // REGULAR_VAARGS 2\n"
                + "REGULAR_VAARGS(a) // REGULAR_VAARGS 1\n"
                + ""
                + "PASTE_ARGS(a, b) // PASTE_ARGS 2\n"
                + "PASTE_ELLIPSIS(a, b) // PASTE_ELLIPSIS 2\n"
                + "PASTE_ELLIPSIS(a) // PASTE_ELLIPSIS 1\n"
                + "PASTE_VAARGS(a, b) // PASTE_VAARGS 2\n"
                + "PASTE_VAARGS(a) // PASTE_VAARGS 1\n";
        LOG.info("Input is:\n" + input);
        Preprocessor pp = new Preprocessor();
        pp.addFeature(Feature.KEEPCOMMENTS);
        pp.addInput(new StringLexerSource(input, true));
        Reader r = new CppReader(pp);
        String output = CharStreams.toString(r).trim();
        LOG.info("Output is:\n" + output);
        assertEquals("foo(a, b) // REGULAR_ARGS 2\n"
                + "foo(a, b) // REGULAR_ELLIPSIS 2\n"
                + "foo(a, ) // REGULAR_ELLIPSIS 1\n"
                + "foo(a, b) // REGULAR_VAARGS 2\n"
                + "foo(a, ) // REGULAR_VAARGS 1\n"
                + "foo(a,b) // PASTE_ARGS 2\n"   // cpp outputs a warning and a space after the comma, similar below.
                + "foo(a,b) // PASTE_ELLIPSIS 2\n"
                + "foo(a) // PASTE_ELLIPSIS 1\n"
                + "foo(a,b) // PASTE_VAARGS 2\n"
                + "foo(a) // PASTE_VAARGS 1", output);
    }
}