Scala, importing class - scala

I have two files:
logic.scala and main.scala
logic.scala contains one class and main.scala have one class with method main (to run it). And I want to import a class from logic.scala and use this class to create object(s) and work with them.
How to import and compile it in proper way?

logic.scala code
package logic
class Logic{
def hello = "hello"
}
main.scala code
package runtime
import logic.Logic // import
object Main extends Application{
println(new Logic hello) // instantiation and invocation
}
compile the files with scalac
scalac *.scala
run your application with scala
scala -cp . runtime.Main

Related

Scala test cannot find any test to run

I am trying to run some test in scala play framework. Here is the code that I have written
package com.sentrana.mmcore.integrationTests
import org.specs2.mutable._
import org.specs2.runner._
import org.junit.runner._
import play.api.test._
import play.api.test.Helpers._
import play.api.libs.json._
class ApplicationSpec extends Specification with Tags {
"Application" should {
"work" in new WithApplication {
route(FakeRequest(GET, "/boum")) must beNone
}
}
}
This is the command that I am using to run the test -
test-only com.sentrana.mmcore.integrationTests.ApplicationSpec
I am not getting any error. But it is showing me that 0 tests have been.
If you add you test using IntellijIDEA make sure that the file has .scala extension. The IDE does not show errors but the activator cannot find the test because it is searching for scala files.

ScalaTest Plus not recognizing tests

I've been tasked to update and write a series of tests on an app in Scala Play, a language and framework I'm unfamiliar with. Part of what I'd like to do is integrate the ScalaTestPlus library. To get started I have been following the following tutorial:
https://www.playframework.com/documentation/2.2.x/ScalaTestingWithScalaTest
Unfortunately I am not getting very far. I have added a new unit test file to the tests folder:
import org.scalatestplus.play._
class StackSpec extends PlaySpec {
"A Test" must {
"pass" in {
assert(1 == 1)
}
"Fail" in {
assert(1 != 1)
}
}
}
and I have updated my build.sbt to include the scalatestplus library
"org.scalatestplus" % "play_2.37" % "1.2.0" % "test"//,
Using Activator, I am trying to run my test file with test-only. Everything compiles without errors, but activator is not finding any tests
[info] No tests were executed.
I don't believe the issue is with activator, since I can run old test files (from the previous engineer) using the test and test-only commands. A quick sample of one of the previous (working) test files:
import java.util.concurrent.TimeUnit
import com.sun.xml.internal.bind.v2.TODO
import scala.collection.JavaConverters._
import controllers.Application
import models.{Item, PriorityBucket}
import play.api.test._
class WebSpec extends PlaySpecification {
"Home page" should {
"do something" in new WithSeleniumDbData(TestUtil.testApp) {
Redacted.deleteAll()
val ObId = TestUtil.create(Some(PriorityBucket.Low),
Some(Application.ENGLISH))
val item = Item.find(ItemId).get
browser.goTo("/")
browser.await().atMost(2,
TimeUnit.SECONDS).until(Selectors.all_obs).isPresent
}
Any ideas where I've gone astray? Thanks in advance for the help!
I am using scala 2.11
I am using play 2.3.7
EDIT: Possibly relevant, I switched the extension from PlaySpec to FlatSpec and saw the following error when compiling:
SampleSpec.scala:10: value in is not a member of String
[error] "pass" in {
I made sure to import FlatSpec as well, which has me a bit confused--is FlatSpec a member of ScalaTest but not a member of ScalaTestPlus, I don't see why else the compilation would fail.
UPDATE: To further investigate the issue I spun up a brand new Play app and copied over my sample test. After some tooling around with versions I've been able to get my test to run on the activator test command with the rest of the suite. Unfortunately, any other commands like test-only are still returning no tests run.
For those following I ran across the issue...the class name in this case needed to be identical to the file name, otherwise test-only cannot locate it.

How to compile scala swing app?

I'm using:
% scalac -version
Scala compiler version 2.9.1 -- Copyright 2002-2011, LAMP/EPFL
on Ubuntu 12.04.
This code is saved in HelloGui.scala:
import scala.swing._
object HelloGui extends SimpleSwingApplication {
def top = new MainFrame {
title = "Hello World GUI"
contents = new Button {
text = "Click me"
}
}
}
When I try to compile this I get:
% scalac HelloGui.scala
HelloGui.scala:1: error: object swing is not a member of package scala
import scala.swing._
^
one error found
I've tried using import swing._ (it isn't clear from the tutorials which import path I need to use with this version of scala), and I get:
% scalac HelloGui.scala
HelloGui.scala:1: error: not found: object swing
import swing._
^
one error found
When I look in /usr/share/java, I see scala-swing-2.9.1.jar and scala-swing.jar as a symlink to it, so it seems like the libraries are present?
Am I missing a compiler flag or is there another package I need to install?
The compiler needs to have the path to the swing jar passed explicitly. This works:
% scalac -classpath /usr/share/java/scala-swing.jar HelloGui.scala

Scala -- not a member of package

I am learning Scala so bear with me if this is a stupid question.
I have this package and a class (teared it down to most simplistic version):
package Foo {
class Bar {}
}
then in main.scala file I have:
import Foo.Bar
object test {
def main() {
val b = new Bar()
}
}
Why am I getting this:
test.scala:1: error: Bar is not a member of Foo
It points at the import statement.
scalac is the scala compiler. Foo.bar needs to have been compiled for you to use it, so you can't just run your main.scala as a script.
The other mistake in your code is that the main method needs to be
def main(args: Array[String]) { ...
(or you could have test extends App instead and do away with the main method).
I can confirm if you put the two files above in an empty directory (with the correction on the main method signature) and run scalac * followed by scala test it runs correctly.
The most likely explanation is that you did not compile the first file, or you are doing something wrong when compiling. Let's say both files are in the current directory, then this should work:
scalac *.scala
It should generate some class files in the current directory, as well as a Bar.class file in the Foo directory, which it will create.
To quickly test a scala code in IntelliJ (with the Scala plugin), you can simply type Ctrl+Shift+F10:
Note that for testing a Scala class, you have other choices, also supported in IntelliJ:
JUnit or TestNG
ScalaTest

Scala: Importing packages into package objects

I'm having trouble importing packages into package objects. It didn't seem to work in Eclipse and so I switched to intellij. At one point the feature seemed to be working and so I created package objects for most packages. Now it doesn't seem to be working at all. Here's a package object in file package.scala, the package file itself compiles fine:
package rStrat.rSwing
package testSw //Edited for clarity
object testSw
{
import rStrat._
import rSwing.topUI._
}
and here's a class file from the same module and package.
package rStrat.rSwing.testSw
object MainTest {
def main(args: Array[String])
{
val testApp = new AppWindow //Appwindow is a member of topUI
testApp.open
}
}
It works fine if I import the topUI package straight into the MainTest file. it makes no difference whether I try and import the whole package or a particular class. is this legal scala? Is the problem with the IDEs?
I'm using Scala 2.92 Final, Intellij 11.1.1, JDK 1.6.0_31, Eclipse 3.7.2
Scala does not have first class imports. Where a package can only contain declarations of classes and traits, a package object can contain any other valid Scala declaration like var, val, def, type (type alias), implicit stuff. And although as in any object you can import things, they don't transitively propagate to the rest of the package and are thus not visible to the rest of the world.
This creates the object rStrat.rSwing.testSw.testSw:
package rStrat.rSwing
package testSw //Edited for clarity
object testSw
This creates the package object rStrat.rSwing.testSw.testSw:
package rStrat.rSwing
package testSw //Edited for clarity
package object testSw
This creates the package object rStrat.rSwing.testSw:
package rStrat.rSwing
package object testSw
It's the last one you want.