ebook img

Programming Alexa Skills for the Amazon Echo PDF

76 Pages·2017·6.11 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Programming Alexa Skills for the Amazon Echo

iMessages, F# Type Providers, Swagger, Cordova MAR APR 2017 5 9 8. $ n a C 5 9 5. $ Programming S U E - N ZI A Alexa Skills G A M R E P O L for the Amazon E V E D T N E D N Echo E P E D N G I N DI A E L E H T m - o c g. a m e d o c Xamarin versus Cordova Azure Skyline and Docker Accessing Data with F# Type Providers TABLE OF CONTENTS Features 8 Data-Driven Testing with Visual Studio 56 How to Know Your Team is Productive and Delivering Quality Does the idea of using an app that’s never been tested give you the willies? It should, and Paul talks about finding the sticking points so you can be Jeffrey takes a look at the state of the industry and comes up with confident that your code works as intended before someone fires it up. some interesting ways to measure efficiency and accuracy. Paul Sheriff Jeffrey Palermo 14 Xamarin versus Cordova 62 Exploring Sticker Packs with iOS 10 These days, you can’t target only one platform with your innovative apps. Sticker packs and emojis are used by everyone from teenagers They have to look good on many types of devices and operating systems. to developers. Jason shows you how these simple tools can be more Sahil looks at how Xamarin and Cordova translate into native apps and than a basic form of communication. how you can use a combination to build even more versatile systems Jason Bender than ever before. Sahil Malik 66 Azure Skyline: Building and Deploying Services to 20 Legal Notes: Selecting and Operating a Legal Azure with .NET Core and Docker Entity for Your User Group Today’s devs have to be nimble. Your app has to work on a multitude of platforms and meet any number of platform-specific demands. Your user group has some serious experts in it and it’s time to share Mike explains how a new tool called Docker can help you deploy the knowledge. If your group wants to host conferences, offer classes, your apps on everything from Linux to .NET and leave you hardly or even go into business, you’ll want John’s advice about what you need breaking a sweat. to start the process. Mike Yeager John Petersen 22 Programming Alexa Skills for the Amazon Echo Columns If you’re looking for a way to computerize your home or add voice controls to your apps, you’re interested in what’s happening with Amazon Echo. Chris introduces you to some basic programming skills for the device and points the way to greatness. 74 Managed Coder: On Coding Management Chris Williams Ted Neward 38 Creating iMessages Apps Did you ever think you’d be programming apps without code? Departments Mohammad show you how, using Apple’s iMessages framework, iOS 10, a little bit of Swift, and some Sticker Packs. Mohammad Azam 6 Editorial 48 Accessing Your Data with F# Type Providers You can access just about any data with type providers, whether in XML, 11 Advertisers Index JSON, or APIs. Rachel shows us how, plus a nifty new Swagger type provider. Rachel Reese 73 Code Compilers US subscriptions are US $29.99 for one year. Subscriptions outside the US pay US $44.99. Payments should be made in US dollars drawn on a US bank. American Express, MasterCard, Visa, and Discover credit cards are accepted. Bill me option is available only for US subscriptions. Back issues are available. For subscription information, send e-mail to [email protected] or contact customer service at 832-717-4445 ext 028. Subscribe online at codemag.com CODE Component Developer Magazine (ISSN # 1547-5166) is published bimonthly by EPS Software Corporation, 6605 Cypresswood Drive, Suite 300, Spring, TX 77379 U.S.A. POSTMASTER: Send address changes to CODE Component Developer Magazine, 6605 Cypresswood Drive, Suite 300, Spring, TX 77379 U.S.A. Canadian Subscriptions: Canada Post Agreement Number 7178957. Send change address information and blocks of undeliverable copies to IBC, 7485 Bath Road, Mississauga, ON L4T 4C1, Canada. 44 Table of Contents ccooddeemmaagg..ccoomm OEDNILTIONREI AQLUICK ID 00 You’re Not Finished Yet I’ve edited a whole lot of books, articles, help files, websites, and white papers about technology. I’ve seen countless products come and go and I’ve been on plenty of teams building apps or software. At the end of the day, there’s one thing I’m sure of: Everything needs to be tested before it’s sent out the door, whether it’s the software, the device, or the words. The whole reason for ZD Labs’ existence was to Whether you rush to add those features or not, Not only will your product (or article, etc.) be bet- find out whether products kept the promises on it’s important to consider that your tester (edi- ter, but you’ll grow, too. their boxes, and to see if a reasonable amount of tor) won’t be the only person to have those use or abuse might break them. We set benchmarks thoughts. They’re merely the first. You need to Editing text is different from testing software, but to compare similar products or functions, and we consider the cost of delivering something that it’s the same principle. You want users to effort- brought in freelancers with widely varied skill sets to might easily be seen as incomplete, even if you lessly read the words and use the product. You need see how much knowledge was needed to succeed— disagree. to know if the dialog box you see as the gateway to or fail. Our purpose wasn’t to help the manufactur- your product contains a mystifying set of text boxes ers, although that was probably a result. We were I’ve seen more unpolished text than software as or if the search function produces random results. trying to inform magazine readers so they could an editor, but it works the same way. If you’re You don’t want people to struggle to use your prod- make intelligent purchases and use the products in writing the help files or UI, or even a nice article, uct; you want a clear flow of information or func- an advantageous way. it’s important that your first reader understands tion, and clear help files in case they get stuck. exactly what you mean. Instructions are not the No matter how brilliant you are as a software de- time to get fancy or play with hyperbole or other I once worked at ZD Labs, which supported all 12 signer, you need someone to be your first user who literary devices. Oh, you’re welcome to be humor- of Ziff-Davis Publishing’s tech magazines. Our group can provide the kind of feedback that helps you de- ous, clever, or even charming (if you are those took hardware and software and did our best to liver a great product before it goes public. It’s the things), but not to the point that the reader break it. We built devices that held laptops pre- same if you’re writing about technology or providing thinks about YOU rather than the product. cariously over various surfaces and dropped them the supporting documents that ship with the prod- repeatedly from a specific and controlled height. uct. You can use (or reread) your work again and A good tester or editor will know what you meant We wired keyboards and spilled various substances again, but you still know what the original intention and be able to help you produce the result that (soda, water, coffee, etc.) on them until they failed, was, and logical lapses won’t jump out at you the you had in mind. You might even be a pretty and measured how much electricity zapped the key- way they would to someone who’s not using your good tester or editor yourself. But there’s a dan- board and whether it went with a whimper or a bang. brain. You need someone to intentionally break your ger in thinking that you can be your own tester We did the opposite of what every well-intentioned product and then give you feedback. or editor. You know that old adage, “The lawyer instruction told us to do. We had a Faraday Cage to who represents himself has a fool for a client,” deliberately electrocute computers and a completely What if your first user (or editor) has different taste right? soundproof room where we could measure how loud or disagrees with your design? What if they don’t that fan keeping your hard drive cool really was or understand your goal, or worse, what if they inject A good dev or writer can certainly make a decent how much noise a room full of mainframes made. In their own ideas into your work? That’s the second pass at his own work, tweaking here and tidy- short, we had a whole lot of fun. half of having your work tested or edited. As impor- ing there. And that should happen, every time, tant as it is to get feedback, it’s just as important to whether you’re writing code or text. But it’s a The object wasn’t really to have fun but to use the know how to respond to it. very bad idea to deliver a product that only you product in the worst possible way in a controlled have tested or edited. environment so that the various tragedies could be Let’s say that you build an app that measures hik- measured. We barely read, entirely skipped, or ob- ing paths. You track whether the paths are also It’s the weird interpretations other people make stinately misinterpreted instructions. We researched suitable for horses, bicycles, or small motor vehi- that allow those flashes of inspiration and insight statistics—how many seated users knocked a laptop cles, you’ve got elevation change, and you track and make your work better. That’s how you grow. off their laps or dropped one while running through distance and give a letter grade for difficulty. You’ll never be finished growing if you’re good at an airport. We even had a rubber finger on a stick Your beta user wants a bigger picture—parking what you do. that poked the same ke, around the clock for weeks availability, running and hiking clubs that use on end until the key broke. the park, handicapped access, bathroom and wa- Post Script: One of my editor friends suggested a ter availability, and so forth. Dr. Dobb’s Journal frequent blogger’s posts about We studied whether a non-technical person could testing. The Dr. Dobb’s site went dark in 2014, figure out how to use a piece of software. To per- Those are good ideas, of course, but perhaps that’s but the articles, and many new ones by Michael form such a test, I built a database using FoxPro not the app you’re building. Or perhaps those are on Hunter, are available here: http://www.thebraidy that tracked the timecards and contract lengths the wish list (backlog) for V2, but you’ve got a dead- tester.com/. I’ve got more than a hundred articles for ZD Labs’ team of freelancers. I enjoyed it line to meet. It’s your job to figure out whether the on writing for technical people on my website, at enough that I next volunteered to build a much app’s users need those things or whether they’re www.MelanieSpiller.com. more involved database using an Oracle product just bells and whistles. You might have to do a cost (with a lot of help) to set up a software and lit- analysis to make this decision or maybe you already Melanie Spiller erature library at the lab. know the right thing to do. 66 Editorial codemag.com ONLINE QUICK ID 1703021 Data-Driven Testing with Visual Studio Every developer needs to test their own code or have it tested by someone else. Many developers aren’t great at testing their own work. The main reason is that we tend to test only the “happy path” through the functionality that we wrote. We often avoid testing the boundaries of our code, such as invalid inputs, exceptions that might occur, etc. One way to become a better tester is to start writing unit tests. Although it takes You also save the time that you normally eat up when you more time up-front to write unit tests, it saves a ton of do your own testing by pressing F5 to start debugging in time if you have to regress test changes to existing fea- Visual Studio, waiting for the compile process, and click- tures. ing through a half dozen menus to finally get to the point where you want to start testing. As you know, this can Starting with Visual Studio 2008, Microsoft added a sometimes take 15 seconds to a minute, depending on unit testing framework into Visual Studio. One of the how large your application is. By unit testing instead, great features of the unit testing tools in Visual Stu- you can run many tests in just a couple of seconds. This dio is the ability to use a data-driven approach. There adds up to saving many hours over your complete devel- Paul D. Sheriff are also several third-party testing frameworks you can opment cycle. http://www.pdsa.com use. In this article, you’ll to learn to retrieve values to be used for testing from SQL Server. Using a data- Automated tests are repeatable, unlike tests run by hu- Paul D. Sheriff is the President of driven approach can significantly reduce the unit tests mans who might forget to test something. You end up PDSA, Inc. PDSA develops custom you must write. with more of your code tested because things won’t be business applications specializing forgotten. Because you’re forced to think of how to test in Web and mobile technolo- your code while you’re writing it, you write better code. gies. PDSA, founded in 1991, has Why You Should Test Your Code You also save time on the set-up and the tear down of successfully delivered advanced We all know that we need to test code prior to putting it the tests because these tasks can also be automated. As custom application software to into production. Testing ensures that the code you write you can see, there are many advantages to an automated a wide range of customers and works as expected. It’s not enough to check that it works approach over human testing. diverse industries. With a team of as expected, but you must also check it under varying cir- dedicated experts, PDSA delivers cumstances and with different inputs. For example, what Of course, there are disadvantages to the automated ap- cost-effective solutions, on-time and on-budget, using innovative if someone takes the database offline? Will your code proach as well. First, it does take more time up-front to tools and processes to better recover gracefully from this exception? Does your code develop these tests. Automated tests are only as good as manage today’s complex and inform the user that there’s a problem with the database the person who develops the tests. The tools to test user competitive environment. connection in a friendly and easily understood manner? interfaces are not always great, and they require that you Paul is also a Pluralsight author. All of these questions need to be answered in the testing purchase third-party testing tools. Then again, if you’re Check out his videos at of your code. You need to simulate these conditions so using good n-tier techniques, MVC, MVP, MVVM, or similar http://www.pluralsight.com/ you can test your exception code. design patterns, there should be very little UI for you to author/paul-sheriff. test anyway. Performing the act of testing often helps you improve the quality of your code. As you think about the various The FileExists Method scenarios that can go wrong, you start to add additional code to handle these scenarios. This leads to your code For this article, you’re going to build a method that being more robust and ultimately more maintainable and checks to see whether a file exists. You’re then going to user-friendly. You’ll find that taking time to test your build unit tests to check each type of input that you can code will make you a better programmer in the long run. pass to this method. Next, you’re going to replace all of Your boss and your end-users will appreciate the extra those unit tests with a single unit test that retrieves the effort as well. various inputs from a database table. To start, create a new Class Library project in Visual Automated Testing Studio using C#. Set the name of this class library Instead of you or a QA person doing all the testing, try to project to MyClasses. Rename the Class1.cs file created automate as much as possible, called unit testing. Unit by Visual Studio to FileProcess. Add a method in this testing means that you write a program, or more likely, a class called FileExists, as shown in the following code series of programs, to test each line of code and ensure snippet: that it operates properly under all circumstances. Unit testing has become very prevalent in today’s professional public bool FileExists(string fileName) { software shops. if (string.IsNullOrEmpty(fileName)) { throw new ArgumentNullException("fileName"); Although this sounds like a lot of work, and it is more } work up front, you’ll more than make up that time by avoiding multiple regression tests that have to be done return File.Exists(fileName); by a human. You can even automate the set-up and tear- } down of databases, files, etc. With the correct architec- ture, you can automate almost all of the various inputs a This is a very simple method, yet it requires at least three human tester would have to enter by hand. unit test methods to ensure that this method works with 8 Data-Driven Testing with Visual Studio codemag.com all possible inputs. The three possible values you can in a null value or a blank value to the FileExists method. pass to the fileName parameter are: An ArgumentNullException is thrown from the FileExists method if a null or blank value is passed to the method. • A file name that exists There are two ways to handle this thrown exception. • A file name that does not exist First, add an ExpectedException attribute after the Test- • A null or empty string Method attribute on the method, as shown in the follow- ing code snippet. Create a Test Project [TestMethod] Right-mouse click on your MyClasses solution and choose [ExpectedException(typeof(ArgumentNullException))] Add -> New Project. From the list of templates, click on public void FileExistsTestNullWithAttribute() { the Visual C# -> Test -> Unit Test Project. Set the Name FileProcess fp = new FileProcess(); to MyClassesTest. Click the OK button. Rename the Unit- Test1.cs file to FileProcessTest.cs. You’re going to test fp.FileExists(""); the method in your MyClasses class library, so you need } to add a reference to that project. Right-mouse click on the References folder in the MyClassesTest project and The second way to handle this exception is to wrap up the select MyClasses. Add the following using statement at call to the FileExists method within a try…catch block. the top of the FileProcessTest.cs file. The catch block should check to see if the exception is an ArgumentNullException. If it is, don’t do anything to using MyClasses; tell the unit test framework that this test succeeded. If no exception is thrown, there’s something wrong with It’s now time to start writing the unit tests to create each the logic and you should call the Assert.Fail() method of the three possible inputs identified for this method. to signal to the unit test framework that this test failed. The first one is to test that a file exists. Add the code Let’s use this second method because this is what you’ll shown below to the FileProcessTest class. Feel free to do when you go to a data-driven approach. change the drive letter, path, and file name to a file that exists on your computer. [TestMethod] public void FileExistsTestTrue() { FileProcess fp = new FileProcess(); bool fromCall; fromCall = fp.FileExists(@"C:\Windows\Regedit.exe"); Assert.AreEqual(true, fromCall); } After adding this code, right-mouse click in your code window and choose Run Tests from the context-sensitive menu that appears. After the code runs, a Test Explorer window appears with the results of the test. If the file exists, the window should display something that looks like Figure 1. The next method to write tests for a file that doesn’t ex- ist. Create a method in your FileProcessTest class to test this condition. Write the code shown in the following code snippet. [TestMethod] public void FileExistsTestFalse() { FileProcess fp = new FileProcess(); bool fromCall; fromCall = fp.FileExists(@"C:\BadFileName.txt"); Assert.AreEqual(false, fromCall); } Once again, run these tests and you should now see two passed tests in your Test Explorer window. Add the fi- nal test to the FileProcessTest class to test if you pass Figure 1: Test results appear in the Test Explorer window. 9 codemag.com Data-Driven Testing with Visual Studio [TestMethod] 4. Add a DataSource attribute to your test method. public void FileExistsTestNull() { 5. Add a config file to hold a connection string and FileProcess fp = new FileProcess(); table name to use. try { Create a Table to Hold Test Data fp.FileExists(""); Because you created three test methods, you’re going to Assert.Fail(); need a table with three rows, one for each test. Each test } passes one value, and returns a Boolean value, so the catch (ArgumentNullException) { table needs at least two fields: FileName and ReturnVal- // The test succeeded ue. Add one more column named CausesException that } you can set to a true or false value on whether you are } expecting an exception to be thrown by the method you are testing. Create this table in a database with the script Run the unit tests one more time, and you should now shown in Listing 1. see three passed tests in the Test Explorer window. Us- ing this approach to testing means that you need to cre- The data in this table that was created from the script ate different methods for each individual file and return matches the hard-coded values in the three-unit test value combination that you wish to test. Extrapolate methods. Table 1 shows the values that are now in your this to a method that has several parameters, multiple table. Feel free to use any specific file names and return If statements or Switch statements, and you can see how values that make sense for your computer. the number of methods you must write can grow substan- tially. This is where a data-driven approach to passing Add Properties and References parameters to your methods reduces the amount of test When the unit test framework creates an instance of a methods you need to create. test class (a class marked with a [TestClases] attribute), it creates a TestContext object. This object contains properties and methods related to testing. One of the Create a Data-Driven Test properties stores the data source information to allow Now that you have a table, let’s create a test method you to access each row in the table you specify for the that can be fed data from the FileProcessTest table you test. To access this TestContext object, you must cre- created previously. There are a few steps to get the data- ate a property named TestContext in each of your test driven test working. classes. 1. Create a table to hold parameter data. private TestContext _TestInstance; 2. Add a reference to System.Data to your test project. public TestContext TestContext 3. Add a TestContext property to your test class. { get { return _TestInstance; } set { _TestInstance = value; } Listing 1: Create a table to hold test values } CREATE SCHEMA tests A DataTable is created from the table you create and a GO DataRow property is exposed on your TestContext prop- erty. In order to use this property, you must add a refer- CREATE TABLE tests.FileProcessTest ence to the System.Data.dll. Go ahead and right-mouse ( click on your test project’s References folder and select FileName varchar(255) NULL, Add Reference… from the menu. Then select Assemblies ExpectedValue [bit] NOT NULL, -> Framework, locate the System.Data assembly, and CausesException [bit] NOT NULL ) add it to your project. GO Create Data-Driven Test Method INSERT INTO tests.FileProcessTest Add a new method to your FileProcessTest class that looks VALUES ('D:\Exists.txt', 1, 0); like the following code snippet. Don’t split the connec- tion string across multiple lines, though; I had to format INSERT INTO tests.FileProcessTest it to fit within the column width of this article. You also VALUES ('D:\NotExists.txt', 0, 0); need to modify the name of your server and the database in which you created the FileProcessTest table. INSERT INTO tests.FileProcessTest VALUES (null, 0, 1); [TestMethod()] [DataSource("System.Data.SqlClient", "Server=Localhost; FileName ReturnValue CausesException Database=Sandbox; Integrated Security=SSPI", C:\Windows\Regedit.exe true false "tests.FileProcessTest", C:\NotExists.xls false false DataAccessMethod.Sequential)] NULL false true public void FileExistsTestFromDB() { Table 1: Data for the FileProcessTest table } 10 Data-Driven Testing with Visual Studio codemag.com

Description:
Tammy Ferguson . MacOS, Java for Android, and C# and XAML for Windows. ence between writing an iOS app using Xamarin and C# and using
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.