github.com/nhannv/mattermost-server@v5.11.1+incompatible/app/svg_test.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"fmt"
     8  	"io"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  func generateSVGData(width int, height int, useViewBox bool, useDimensions bool, useAlternateFormat bool) io.Reader {
    14  	var (
    15  		viewBoxAttribute = ""
    16  		widthAttribute   = ""
    17  		heightAttribute  = ""
    18  	)
    19  	if useViewBox == true {
    20  		separator := " "
    21  		if useAlternateFormat == true {
    22  			separator = ", "
    23  		}
    24  		viewBoxAttribute = fmt.Sprintf(` viewBox="0%s0%s%d%s%d"`, separator, separator, width, separator, height)
    25  	}
    26  	if useDimensions == true && width > 0 && height > 0 {
    27  		units := ""
    28  		if useAlternateFormat == true {
    29  			units = "px"
    30  		}
    31  		widthAttribute = fmt.Sprintf(` width="%d%s"`, width, units)
    32  		heightAttribute = fmt.Sprintf(` height="%d%s"`, height, units)
    33  	}
    34  	svgString := fmt.Sprintf(`<svg%s%s%s></svg>`, widthAttribute, heightAttribute, viewBoxAttribute)
    35  	return strings.NewReader(svgString)
    36  }
    37  
    38  func TestParseValidSVGData(t *testing.T) {
    39  	var width, height int = 300, 300
    40  	validSVGs := []io.Reader{
    41  		generateSVGData(width, height, true, true, false),  // properly formed viewBox, width & height
    42  		generateSVGData(width, height, true, true, true),   // properly formed viewBox, width & height; alternate format
    43  		generateSVGData(width, height, false, true, false), // missing viewBox, properly formed width & height
    44  		generateSVGData(width, height, false, true, true),  // missing viewBox, properly formed width & height; alternate format
    45  	}
    46  	for index, svg := range validSVGs {
    47  		svgInfo, err := parseSVG(svg)
    48  		if err != nil {
    49  			t.Errorf("Should be able to parse SVG attributes at index %d, but was not able to: err = %v", index, err)
    50  		} else {
    51  			if svgInfo.Width != width {
    52  				t.Errorf("Expecting a width of %d for SVG at index %d, but it was %d instead.", width, index, svgInfo.Width)
    53  			}
    54  
    55  			if svgInfo.Height != height {
    56  				t.Errorf("Expecting a height of %d for SVG at index %d, but it was %d instead.", height, index, svgInfo.Height)
    57  			}
    58  		}
    59  	}
    60  }
    61  
    62  func TestParseInvalidSVGData(t *testing.T) {
    63  	var width, height int = 300, 300
    64  	invalidSVGs := []io.Reader{
    65  		generateSVGData(width, height, false, false, false), // missing viewBox, width & height
    66  		generateSVGData(width, 0, false, true, false),       // missing viewBox, malformed width & height
    67  		generateSVGData(300, 0, false, true, false),         // missing viewBox, malformed height, properly formed width
    68  	}
    69  	for index, svg := range invalidSVGs {
    70  		_, err := parseSVG(svg)
    71  		if err == nil {
    72  			t.Errorf("Should not be able to parse SVG attributes at index %d, but was definitely able to!", index)
    73  		}
    74  	}
    75  }