PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms 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. */
  18. package org.fusesource.scalate.util
  19. import org.fusesource.scalate.FunSuiteSupport
  20. import java.io.File
  21. import IOUtil._
  22. class FileTest extends FunSuiteSupport {
  23. test("using rich file API to navigate") {
  24. val f: File = baseDir
  25. val sources = f / "src" / "main" / "scala"
  26. assertResult(true) { sources.exists }
  27. val f2: File = sources
  28. assertResult(true) { f2.exists }
  29. info("created file: " + sources.file)
  30. }
  31. test("getting text of a file") {
  32. val file: File = baseDir / "src/test/resources/dummy.txt"
  33. val t = file.text.trim
  34. assertResult("hello world!") { t }
  35. info("Loaded file: " + file + " as text: " + t)
  36. }
  37. test("getting text of a file with include") {
  38. val file: File = baseDir / "src/test/resources/dummyWithInclude.txt"
  39. val t = IOUtil.loadTextFile(file).trim.replace("\r", "")
  40. assertResult("My header 1\nhello world!") { t }
  41. info("Loaded file: " + file + " as text: " + t)
  42. }
  43. test("getting text of a file with two includes") {
  44. val file: File = baseDir / "src/test/resources/dummyWithTwoIncludes.txt"
  45. val t = IOUtil.loadTextFile(file).trim.replace("\r", "")
  46. assertResult("My header 1\nMy Second Header\ngood bye world!") { t }
  47. info("Loaded file: " + file + " as text: " + t)
  48. }
  49. test("getting text of a file with include in the middle") {
  50. val file: File = baseDir / "src/test/resources/dummyWithIncludeInMiddle.txt"
  51. val t = IOUtil.loadTextFile(file).trim.replace("\r", "")
  52. assertResult("hello world!\nMy header 1\nAFTER WORLD!") { t }
  53. info("Loaded file: " + file + " as text: " + t)
  54. }
  55. test("getting text of a file with nested include") {
  56. val file: File = baseDir / "src/test/resources/dummyWithNestedInclude.txt"
  57. val t = IOUtil.loadTextFile(file).trim.replace("\r", "")
  58. assertResult("My header 1\nhello world!\nEnd of 2012 is here") { t }
  59. info("Loaded file: " + file + " as text: " + t)
  60. }
  61. test("working with names") {
  62. val file = baseDir / "foo.txt"
  63. info("name: " + file.name + " extension: " + file.extension)
  64. assertResult("txt", "extension") { file.extension }
  65. assertResult("foo", "nameDropExtension") { file.nameDropExtension }
  66. }
  67. test("Finding files") {
  68. assertResult(None) {
  69. baseDir.recursiveFind(_.name == "doesNotExist.xml")
  70. }
  71. assertResult(Some(new File(baseDir, "src"))) {
  72. baseDir.recursiveFind(_.name == "src")
  73. }
  74. assertResult(Some(new File(baseDir, "src/test/scala/org/fusesource/scalate/util/FileTest.scala"))) {
  75. baseDir.recursiveFind(_.name == "FileTest.scala")
  76. }
  77. }
  78. test("relative URIs") {
  79. assertResult("src") {
  80. (baseDir / "src").relativeUri(baseDir)
  81. }
  82. assertResult("src/test/scala/org/fusesource/scalate/util/FileTest.scala") {
  83. new File(baseDir, "src/test/scala/org/fusesource/scalate/util/FileTest.scala").relativeUri(baseDir)
  84. }
  85. }
  86. assertNameSplit("foo", "foo", "")
  87. assertNameSplit("foo.", "foo", "")
  88. assertNameSplit("foo.txt", "foo", "txt")
  89. assertNameSplit("foo.bar.txt", "foo.bar", "txt")
  90. assertNameSplit(".txt", "", "txt")
  91. assertNameSplit(".", "", "")
  92. def assertNameSplit(name: String, expectedName: String, expectedExt: String): Unit = {
  93. test("splitName: " + name) {
  94. info("Name " + name + " -> name: " + Files.dropExtension(name) + " extension: " + Files.extension(name))
  95. assertResult(expectedExt, "extension") { Files.extension(name) }
  96. assertResult(expectedName, "name without extension") { Files.dropExtension(name) }
  97. }
  98. }
  99. }