Skip to content

Commit

Permalink
BlockFormat finished
Browse files Browse the repository at this point in the history
  • Loading branch information
paradigmatic committed Jul 3, 2011
1 parent af78d01 commit 8d8a590
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 10 deletions.
48 changes: 39 additions & 9 deletions src/io/BlockFormat.scala
Expand Up @@ -26,19 +26,49 @@ import scala.util.parsing.combinator._
/**
* Encodes the default block format
*/
object BlockFormat extends ImportFormat {

object BlockFormat extends Format {

def toText( configuration: Configuration ) = write( configuration.data )

def fromText( s: String ) = new Parser().parse( s )


private def splitKey( s: String ) = s.split("""\.""").toList
private def joinKey( ss: List[String] ) = ss.mkString(".")
private def writeEntry(k:String,v:String,ind:Int,out:StringBuffer) {
out.append( " " * ind )
.append( k ).append(" = ").append(v).append("\n")
}

/*def toText( configuration: Configuration ) = {
val out = new StringBuilder
val data = configuration.data
for( k <- data.keySet.toList.sorted ) {
out.append(k).append(" = ").append( data(k) ).append(sep)
private def write(
map: Map[String,String],
indents: Int = 0
): String = {
val out = new StringBuffer
var blocks = Map[String,Map[String,String]]()
for( (k,v) <- map ) {
splitKey(k) match {
case el :: Nil => {
writeEntry(k,v,indents,out)
}
case first :: rest => {
val subKey = joinKey(rest)
blocks += first ->
( blocks.getOrElse( first, Map() ) + ( subKey -> v) )
}
case _ =>
}
}
for( (k,block) <- blocks ) {
out.append(" "*indents).append(k).append(" {").append("\n")
out.append( write( block, indents + 1 ) )
out.append(" "*indents).append("}").append("\n")
}
out.toString
}*/
}



def fromText( s: String ) = new Parser().parse( s )

/** Parser exceptions */
case class ParserException(s: String) extends Exception(s)
Expand Down
40 changes: 39 additions & 1 deletion test-src/io/BlocFormatSpec.scala
Expand Up @@ -5,6 +5,32 @@ import configrity.ValueConverters._
import configrity.io.BlockFormat
import configrity.io.BlockFormat._

class BlockFormatSpec extends FlatSpec with ShouldMatchers{

"The block format" can "write and read an empty Configuration" in {
val config = Configuration( Map() )
fromText( toText( config ) ) should be (config)
}

it can "write and read a Configuration" in {
val config = Configuration(
Map("foo"->"FOO", "bar"->"1234", "baz"->"on")
)
fromText( toText( config ) ) should be (config)
}

it can "write and read a Configuration with nested blocks" in {
val config = Configuration(
Map(
"foo.gnats.gnits"->"FOO",
"bar.buzz"->"1234",
"bar.baz"->"on"
)
)
fromText( toText( config ) ) should be (config)
}

}

class BlockFormatParserSpec extends FlatSpec with ShouldMatchers{

Expand Down Expand Up @@ -245,6 +271,18 @@ class BlockFormatParserSpec extends FlatSpec with ShouldMatchers{
}

it should "merge blocks with same key" in {
pending
val s =
"""
# Example
foo = true
block {
bar = 2
}
block {
bar = x
}
"""
val config = parse( s )
config[String]("block.bar") should be (Some("x"))
}
}

0 comments on commit 8d8a590

Please sign in to comment.