File: 01-single-quotes.t

package info (click to toggle)
libsql-tokenizer-perl 0.24-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 176 kB
  • sloc: perl: 103; makefile: 2
file content (109 lines) | stat: -rw-r--r-- 3,842 bytes parent folder | download | duplicates (2)
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
use strict;
use warnings;

use Test::More;

use SQL::Tokenizer;

use constant SPACE => ' ';
use constant COMMA => ',';
use constant NL    => "\n";

my $query;
my @query;
my @tokenized;

my @tests= (
    {
        description => qq{one C style scaped single quote},
        query       => 'INSERT INTO table VALUES( \'scaped \\\' single quote\', \'no quote\' )',
        wanted      => [
            'INSERT', SPACE, 'INTO', SPACE, 'table', SPACE, 'VALUES', '(', SPACE,
            q{'scaped \' single quote'},
            COMMA, SPACE, q{'no quote'}, SPACE, ')'
        ],
    },

    {
        description => qq{no quotes inside string},
        query       => 'INSERT INTO table VALUES( \'no quote\', \'no quote either\' )',
        wanted      => [
            'INSERT', SPACE, 'INTO', SPACE, 'table', SPACE, 'VALUES', '(', SPACE, q{'no quote'},
            COMMA, SPACE, q{'no quote either'},
            SPACE, ')'
        ],
    },

    {
        description => qq{more than one C style escaped single quotes inside string},
        query =>
          q{INSERT INTO logs (program, message) VALUES (:program, 'Something \' with \' a \' lot \' of \' scaped quotes')},
        wanted => [
            'INSERT',   SPACE,    'INTO',    SPACE,
            'logs',     SPACE,    '(',       'program',
            COMMA,      SPACE,    'message', ')',
            SPACE,      'VALUES', SPACE,     '(',
            ':program', COMMA,    SPACE,
            q{'Something \' with \' a \' lot \' of \' scaped quotes'},
            ')'
        ],
    },

    {
        description => qq{more than one C style escaped single quotes inside string, with extra backslashes},
        query =>
          q{INSERT INTO logs (program, message) VALUES (:program, 'Something \' with \' a \' lot \' of \' scaped quotes\\\\\\\\\\\\\\\\')},
        wanted => [
            'INSERT',   SPACE,    'INTO',    SPACE,
            'logs',     SPACE,    '(',       'program',
            COMMA,      SPACE,    'message', ')',
            SPACE,      'VALUES', SPACE,     '(',
            ':program', COMMA,    SPACE,
            q{'Something \' with \' a \' lot \' of \' scaped quotes\\\\\\\\\\\\\\\\'},
            ')'
        ],
    },

    {
        description => qq{SQL style escaped single quotes},
        query       => q{INSERT INTO logs (program) VALUES ('single''quote')},
        wanted      => [
            'INSERT', SPACE, 'INTO', SPACE, 'logs', SPACE, '(', 'program', ')', SPACE, 'VALUES',
            SPACE, '(', q{'single''quote'}, ')'
        ],
    },

    {
        description => qq{SQL style escaped single quotes with surrounding spaces},
        query       => q{INSERT INTO logs (program) VALUES ('single '' quote '' ')},
        wanted      => [
            'INSERT', SPACE, 'INTO', SPACE, 'logs', SPACE, '(', 'program', ')', SPACE, 'VALUES',
            SPACE, '(', q{'single '' quote '' '}, ')'
        ],
    },

    {
        description => qq{C style escaped single quote at end of string},
        query       => q{INSERT INTO logs (program) VALUES ('single '' quote \'')},
        wanted      => [
            'INSERT', SPACE, 'INTO', SPACE, 'logs', SPACE, '(', 'program', ')', SPACE, 'VALUES',
            SPACE, '(', q{'single '' quote \''}, ')'
        ],
    },

    {
        description => qq{multiple SQL style escaped single quotes at end of string},
        query       => q{INSERT INTO logs (program) VALUES ('single '' quote ''''')},
        wanted      => [
            'INSERT', SPACE, 'INTO', SPACE, 'logs', SPACE, '(', 'program', ')', SPACE, 'VALUES',
            SPACE, '(', q{'single '' quote '''''}, ')'
        ],
    },
);

plan tests => scalar @tests;

foreach my $test (@tests) {
    my @tokenized= SQL::Tokenizer->tokenize( $test->{query} );
    is_deeply( \@tokenized, $test->{wanted}, $test->{description} );
}