github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/test_framework/util/util.go (about)

     1  package util
     2  
     3  import (
     4  	. "github.com/balzaczyy/gounit"
     5  	"io/ioutil"
     6  	"math/rand"
     7  	"os"
     8  )
     9  
    10  // util/_TestUtil.java
    11  
    12  // Returns a temp directory, based on the given description. Creates the directory.
    13  func TempDir(desc string) string {
    14  	if len(desc) < 3 {
    15  		panic("description must be at least 3 characters")
    16  	}
    17  	// Ian: I prefer Go's own way to obtain temp folder instead of Lucene's method
    18  	f, err := ioutil.TempDir("", desc)
    19  	if err != nil {
    20  		panic(err)
    21  	}
    22  	f += ".tmp" // add suffix
    23  	CloseAfterSuite(NewCloseableFile(f, SuiteFailureMarker))
    24  	return f
    25  }
    26  
    27  // L264
    28  func NextInt(r *rand.Rand, start, end int) int {
    29  	return r.Intn(end-start) + start
    30  }
    31  
    32  // L314
    33  // Returns random string, including full unicode range.
    34  func RandomUnicodeString(r *rand.Rand) string {
    35  	return randomUnicodeStringLength(r, 20)
    36  }
    37  
    38  // Returns a random string up to a certain length.
    39  func randomUnicodeStringLength(r *rand.Rand, maxLength int) string {
    40  	end := NextInt(r, 0, maxLength)
    41  	if end == 0 {
    42  		// allow 0 length
    43  		return ""
    44  	}
    45  	buffer := make([]rune, end)
    46  	randomFixedLengthUnicodeString(r, buffer)
    47  	return string(buffer)
    48  }
    49  
    50  // Fills provided []rune with valid random unicode code unit sequence.
    51  func randomFixedLengthUnicodeString(random *rand.Rand, chars []rune) {
    52  	for i, length := 0, len(chars); i < length; i++ {
    53  		t := random.Intn(5)
    54  		if t == 0 && i < length-1 {
    55  			// Make a surrogate pair
    56  			// High surrogate
    57  			chars[i] = rune(NextInt(random, 0xd800, 0xdbff))
    58  			// Low surrogate
    59  			i++
    60  			chars[i] = rune(NextInt(random, 0xdc00, 0xdfff))
    61  		} else if t <= 1 {
    62  			chars[i] = rune(random.Intn(0x80))
    63  		} else if t == 2 {
    64  			chars[i] = rune(NextInt(random, 0x80, 0x7ff))
    65  		} else if t == 3 {
    66  			chars[i] = rune(NextInt(random, 0x800, 0xd7ff))
    67  		} else if t == 4 {
    68  			chars[i] = rune(NextInt(random, 0xe000, 0xffff))
    69  		}
    70  	}
    71  }
    72  
    73  // util/TestRuleMarkFailure.java
    74  
    75  type TestRuleMarkFailure struct {
    76  	failures bool
    77  }
    78  
    79  // Check if this object had any marked failures.
    80  func (tr *TestRuleMarkFailure) hadFailures() bool {
    81  	return tr.failures
    82  }
    83  
    84  // Check if this object was sucessful
    85  func (tr *TestRuleMarkFailure) WasSuccessful() bool {
    86  	return tr.hadFailures()
    87  }
    88  
    89  // util/CloseableFile.java
    90  
    91  // A Closeable that attempts to remove a given file/folder
    92  func NewCloseableFile(file string, failureMarker *TestRuleMarkFailure) func() error {
    93  	return func() error {
    94  		// only if there were no other test failures.
    95  		if failureMarker.WasSuccessful() {
    96  			os.RemoveAll(file) // ignore any error
    97  			// no re-check
    98  		}
    99  		return nil
   100  	}
   101  }