PageRenderTime 25ms CodeModel.GetById 15ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/scalate-util/src/test/scala/org/fusesource/scalate/util/FileTest.scala

http://github.com/scalate/scalate
Scala | 135 lines | 82 code | 36 blank | 17 comment | 3 complexity | ab79d8a16208e7463128513fa9b6e410 MD5 | raw file
  1/**
  2 * Copyright (C) 2009-2011 the original author or authors.
  3 * See the notice.md file distributed with this work for additional
  4 * information regarding copyright ownership.
  5 *
  6 * Licensed under the Apache License, Version 2.0 (the "License");
  7 * you may not use this file except in compliance with the License.
  8 * You may obtain a copy of the License at
  9 *
 10 *     http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18package org.fusesource.scalate.util
 19
 20import org.fusesource.scalate.FunSuiteSupport
 21import java.io.File
 22import IOUtil._
 23
 24class FileTest extends FunSuiteSupport {
 25  test("using rich file API to navigate") {
 26    val f: File = baseDir
 27
 28    val sources = f / "src" / "main" / "scala"
 29
 30    assertResult(true) { sources.exists }
 31
 32    val f2: File = sources
 33    assertResult(true) { f2.exists }
 34
 35    info("created file: " + sources.file)
 36  }
 37
 38  test("getting text of a file") {
 39    val file: File = baseDir / "src/test/resources/dummy.txt"
 40
 41    val t = file.text.trim
 42    assertResult("hello world!") { t }
 43
 44    info("Loaded file: " + file + " as text: " + t)
 45  }
 46
 47  test("getting text of a file with include") {
 48    val file: File = baseDir / "src/test/resources/dummyWithInclude.txt"
 49
 50    val t = IOUtil.loadTextFile(file).trim.replace("\r", "")
 51
 52    assertResult("My header 1\nhello world!") { t }
 53
 54    info("Loaded file: " + file + " as text: " + t)
 55  }
 56
 57  test("getting text of a file with two includes") {
 58    val file: File = baseDir / "src/test/resources/dummyWithTwoIncludes.txt"
 59
 60    val t = IOUtil.loadTextFile(file).trim.replace("\r", "")
 61
 62    assertResult("My header 1\nMy Second Header\ngood bye world!") { t }
 63
 64    info("Loaded file: " + file + " as text: " + t)
 65  }
 66
 67  test("getting text of a file with include in the middle") {
 68    val file: File = baseDir / "src/test/resources/dummyWithIncludeInMiddle.txt"
 69
 70    val t = IOUtil.loadTextFile(file).trim.replace("\r", "")
 71
 72    assertResult("hello world!\nMy header 1\nAFTER WORLD!") { t }
 73
 74    info("Loaded file: " + file + " as text: " + t)
 75  }
 76
 77  test("getting text of a file with nested include") {
 78    val file: File = baseDir / "src/test/resources/dummyWithNestedInclude.txt"
 79
 80    val t = IOUtil.loadTextFile(file).trim.replace("\r", "")
 81
 82    assertResult("My header 1\nhello world!\nEnd of 2012 is here") { t }
 83
 84    info("Loaded file: " + file + " as text: " + t)
 85  }
 86
 87  test("working with names") {
 88    val file = baseDir / "foo.txt"
 89
 90    info("name: " + file.name + " extension: " + file.extension)
 91
 92    assertResult("txt", "extension") { file.extension }
 93    assertResult("foo", "nameDropExtension") { file.nameDropExtension }
 94  }
 95
 96  test("Finding files") {
 97    assertResult(None) {
 98      baseDir.recursiveFind(_.name == "doesNotExist.xml")
 99    }
100
101    assertResult(Some(new File(baseDir, "src"))) {
102      baseDir.recursiveFind(_.name == "src")
103    }
104
105    assertResult(Some(new File(baseDir, "src/test/scala/org/fusesource/scalate/util/FileTest.scala"))) {
106      baseDir.recursiveFind(_.name == "FileTest.scala")
107    }
108  }
109
110  test("relative URIs") {
111    assertResult("src") {
112      (baseDir / "src").relativeUri(baseDir)
113    }
114
115    assertResult("src/test/scala/org/fusesource/scalate/util/FileTest.scala") {
116      new File(baseDir, "src/test/scala/org/fusesource/scalate/util/FileTest.scala").relativeUri(baseDir)
117    }
118  }
119
120  assertNameSplit("foo", "foo", "")
121  assertNameSplit("foo.", "foo", "")
122  assertNameSplit("foo.txt", "foo", "txt")
123  assertNameSplit("foo.bar.txt", "foo.bar", "txt")
124  assertNameSplit(".txt", "", "txt")
125  assertNameSplit(".", "", "")
126
127  def assertNameSplit(name: String, expectedName: String, expectedExt: String): Unit = {
128    test("splitName: " + name) {
129      info("Name " + name + " -> name: " + Files.dropExtension(name) + " extension: " + Files.extension(name))
130
131      assertResult(expectedExt, "extension") { Files.extension(name) }
132      assertResult(expectedName, "name without extension") { Files.dropExtension(name) }
133    }
134  }
135}