alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (BasicFrameFormatterSpec.scala)

This example Play Framework source code file (BasicFrameFormatterSpec.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

basicframeformatterspec, binaryframe, binaryframes, core, left, play, play framework, seq, specification, textframe, textframes, websocket

The BasicFrameFormatterSpec.scala Play Framework example source code

/*
 * Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
 */
package play.core.websocket

import org.specs2.mutable.Specification

object BasicFrameFormatterSpec extends Specification {

  "BasicFrameFormatter.textFrame" should {
    "translate strings to TextFrames" in {
      BasicFrameFormatter.textFrame.toFrame("hello") must_== TextFrame("hello")
    }
    "translate TextFrames to strings" in {
      BasicFrameFormatter.textFrame.fromFrame(TextFrame("hello")) must_== "hello"
    }
    "not translate BinaryFrames" in {
      BasicFrameFormatter.textFrame.fromFrame(BinaryFrame(Array[Byte](1, 2, 3))) must throwAn[IllegalArgumentException]
    }
    "say that it handles TextFrames" in {
      BasicFrameFormatter.textFrame.fromFrameDefined(classOf[TextFrame]) must beTrue
    }
    "say that it doesn't handle BinaryFrames" in {
      BasicFrameFormatter.textFrame.fromFrameDefined(classOf[BinaryFrame]) must beFalse
    }
  }

  "BasicFrameFormatter.binaryFrame" should {
    "translate byte arrays to BinaryFrames" in {
      BasicFrameFormatter.binaryFrame.toFrame(Array[Byte](1, 2, 3)) must beLike {
        case BinaryFrame(bytes) => bytes.to[Seq] must_== Seq[Byte](1, 2, 3)
      }
    }
    "translate BinaryFrames to byte arrays" in {
      BasicFrameFormatter.binaryFrame.fromFrame(BinaryFrame(Array[Byte](1, 2, 3))).to[Seq] must_== Seq[Byte](1, 2, 3)
    }
    "not translate TextFrames" in {
      BasicFrameFormatter.binaryFrame.fromFrame(TextFrame("foo")) must throwAn[IllegalArgumentException]
    }
    "say that it handles BinaryFrames" in {
      BasicFrameFormatter.binaryFrame.fromFrameDefined(classOf[BinaryFrame]) must beTrue
    }
    "say that it doesn't handle TextFrames" in {
      BasicFrameFormatter.binaryFrame.fromFrameDefined(classOf[TextFrame]) must beFalse
    }
  }

  "BasicFrameFormatter.mixedFrame" should {
    "translate strings to TextFrames" in {
      BasicFrameFormatter.mixedFrame.toFrame(Left("banana")) must_== TextFrame("banana")
    }
    "translate byte arrays to BinaryFrames" in {
      BasicFrameFormatter.mixedFrame.toFrame(Right(Array[Byte](1, 2, 3))) must beLike {
        case BinaryFrame(bytes) => bytes.to[Seq] must_== Seq[Byte](1, 2, 3)
      }
    }
    "translate TextFrames to strings" in {
      BasicFrameFormatter.mixedFrame.fromFrame(TextFrame("elephant")) must_== Left("elephant")
    }
    "translate BinaryFrames to byte arrays" in {
      BasicFrameFormatter.mixedFrame.fromFrame(BinaryFrame(Array[Byte](1, 2, 3))) must beRight.like {
        case bytes => bytes.to[Seq] must_== Seq[Byte](1, 2, 3)
      }
    }
    "say that it handles BinaryFrames" in {
      BasicFrameFormatter.mixedFrame.fromFrameDefined(classOf[BinaryFrame]) must beTrue
    }
    "say that it handles TextFrames" in {
      BasicFrameFormatter.mixedFrame.fromFrameDefined(classOf[TextFrame]) must beTrue
    }
  }

}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework BasicFrameFormatterSpec.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.