Template-Toolkit

 view release on metacpan or  search on metacpan

t/truncate_cer.t  view on Meta::CPAN

    #--------------------------------------------------------------------
    {
        name   => 'hex entity suffix: … counts as 1 char',
        input  => 'I have much to say on this matter that has previously been said.',
        tmpl   => '[% text | truncate(27, "…") %]',
        expect => 'I have much to say on this…',
    },

    #--------------------------------------------------------------------
    # Plain suffix (no entities) — regression check
    #--------------------------------------------------------------------
    {
        name   => 'plain suffix: ... still works (3 chars)',
        input  => 'The cat sat on the mat and wondered.',
        tmpl   => '[% text | truncate(10) %]',
        expect => 'The cat...',
    },
    {
        name   => 'no truncation needed',
        input  => 'Short',
        tmpl   => '[% text | truncate(10) %]',
        expect => 'Short',
    },
    {
        name   => 'exact length — no truncation',
        input  => 'Hello World',
        tmpl   => '[% text | truncate(11) %]',
        expect => 'Hello World',
    },
    {
        name   => 'len less than suffix — suffix itself truncated',
        input  => 'Hello World',
        tmpl   => '[% text | truncate(2) %]',
        expect => '..',
    },

    #--------------------------------------------------------------------
    # Multiple entities in suffix
    #--------------------------------------------------------------------
    {
        name   => 'two entities in suffix count as 2 chars',
        input  => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
        tmpl   => '[% text | truncate(10, "<>") %]',
        expect => 'ABCDEFGH<>',
    },

    #--------------------------------------------------------------------
    # Mixed plain + entity chars in suffix
    #--------------------------------------------------------------------
    {
        name   => 'mixed suffix: "...…" counts as 4 chars (3 dots + 1 entity)',
        input  => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
        tmpl   => '[% text | truncate(10, "...…") %]',
        expect => 'ABCDEF...…',
    },

    #--------------------------------------------------------------------
    # Entity in input text — should not be split
    #--------------------------------------------------------------------
    {
        name   => 'entity in input text not split mid-reference',
        input  => 'AB&CDEFGHIJ',
        tmpl   => '[% text | truncate(5, "...") %]',
        # visual: A B & C D E F G H I J = 11 visual chars
        # truncate to 5 visual: 2 text + "..." = 5? No: 5 - 3 = 2 visual chars of text
        # visual chars: A, B → "AB..."
        expect => 'AB...',
    },
    {
        name   => 'entity in input text counted as 1 visual char',
        input  => 'A&B<C>DEFGHIJKLMNO',
        tmpl   => '[% text | truncate(6, "...") %]',
        # visual: A & B < C > D E F G H I J K L M N O = 18 visual chars
        # truncate to 6: 6 - 3 = 3 visual chars of text
        # A, &, B → "A&B..."
        expect => 'A&B...',
    },
    {
        name   => 'input with entity exactly at boundary',
        input  => 'ABCD…FGHIJ',
        tmpl   => '[% text | truncate(7, "...") %]',
        # visual: A B C D … F G H I J = 10 visual chars
        # truncate to 7: 7 - 3 = 4 visual chars of text
        # A, B, C, D → "ABCD..."
        expect => 'ABCD...',
    },
    {
        name   => 'input short enough with entities — no truncation',
        input  => 'A&B',
        tmpl   => '[% text | truncate(10) %]',
        # visual length = 3, less than 10
        expect => 'A&B',
    },

    #--------------------------------------------------------------------
    # Edge cases
    #--------------------------------------------------------------------
    {
        name   => 'empty string — no truncation',
        input  => '',
        tmpl   => '[% text | truncate(10) %]',
        expect => '',
    },
    {
        name   => 'entity-only suffix with len=1',
        input  => 'ABCDEFGHIJ',
        tmpl   => '[% text | truncate(1, "…") %]',
        # … is 1 visual char, len=1, so no room for text
        expect => '…',
    },
    {
        name   => 'ampersand not part of entity — counts normally',
        input  => 'Tom & Jerry go to the park and have fun',
        tmpl   => '[% text | truncate(15, "...") %]',
        # "Tom & Jerry " = 12 chars (no entity), "..." = 3 → 15
        expect => 'Tom & Jerry ...',
    },
    {
        name   => 'incomplete entity reference in input — not treated as CER',
        input  => 'AB&notanentity CDEFGH',
        tmpl   => '[% text | truncate(10, "...") %]',



( run in 0.465 second using v1.01-cache-2.11-cpan-71847e10f99 )