go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/testing/assert/results/value.go (about)

     1  // Copyright 2024 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package results
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  )
    21  
    22  // value is a simple (name, value) tuple.
    23  type value struct {
    24  	name string
    25  
    26  	// A verbatimString is rendered literally, anything else
    27  	// is rendered with "%#v"
    28  	value any
    29  }
    30  
    31  // render prints lines as a block of lines.
    32  func (v *value) render() []string {
    33  	if v == nil {
    34  		return nil
    35  	}
    36  	val := ""
    37  	if verbatim, ok := v.value.(verbatimString); ok {
    38  		val = verbatim.String()
    39  	} else {
    40  		val = fmt.Sprintf("%#v", v.value)
    41  	}
    42  	if strings.TrimSpace(val) == "" {
    43  		return []string{fmt.Sprintf("%s%s: <empty>", "  ", v.name)}
    44  	}
    45  	lines := strings.Split(val, "\n")
    46  	return blockLines("  ", v.name, lines)
    47  }
    48  
    49  // if lines has zero lines, returns nil.
    50  // if lines has one line, returns just that line prefixed with prefix and
    51  // title.
    52  // if lines has many lines, returns a prefixed title line followed by
    53  // prefix+"| " prefixed lines.
    54  //
    55  // If lines has multiple blank lines in a row, they are collapsed into a single
    56  // blank line.
    57  //
    58  // If lines ends with a blank line, it is dropped.
    59  func blockLines(prefix, title string, lines []string) (ret []string) {
    60  	if len(lines) == 0 {
    61  		return nil
    62  	}
    63  	if len(lines) == 1 {
    64  		if strings.TrimSpace(lines[0]) == "" {
    65  			return
    66  		}
    67  		return []string{fmt.Sprintf("%s%s: %s", prefix, title, lines[0])}
    68  	}
    69  	buf := make([]string, 0, len(lines)+1)
    70  	buf = append(buf, fmt.Sprintf("%s%s:", prefix, title))
    71  	var needBlank bool
    72  	for _, line := range lines {
    73  		if strings.TrimSpace(line) == "" {
    74  			needBlank = true
    75  			continue
    76  		}
    77  		if needBlank {
    78  			buf = append(buf, fmt.Sprintf("%s|", prefix))
    79  			needBlank = false
    80  		}
    81  		buf = append(buf, fmt.Sprintf("%s| %s", prefix, line))
    82  	}
    83  	if len(buf) > 0 {
    84  		// could be all blank lines... this would be pretty weird, but may as well be
    85  		// consistent.
    86  		ret = buf
    87  	}
    88  	return
    89  }